0

So, I think I know what the RegEx + symbol does (looks for the preceding character 1 or more times). I think I get how to use it...

echo "This is a good sentence." | awk '/go+d/ {print $0}'

(finds good, gooood, gooooooood)

But, I'm having trouble coming up with a practical, objective reason for wanting to look for this kind of repetition. I need scenarios to give its purpose some context for explanation to others.

Thanks for helping and letting me lean on your knowledge and experiences.

2 Answers2

4

You're thinking about individual characters when you'll find that + is much more useful with character classes...

  • Whitespace: \s+
  • Non-numerical characters: [^0-9]+
  • A word: \w+
  • Non-empty string: .+
  • Etc.

Practical examples should be fairly easy to think of with these. Perhaps I want to find only lines that have indented text: ^\s+\S. Or I might want to find all non-empty lines: ^.+$. I can come up with more if you need them. ;)

B Layer
  • 5,171
3

Here's a real-world instance of damage caused by using * instead of + (mostly because OP didn't realise that they would end up using regexes instead of wildcards): Why did this command delete every package?

Arguments to apt-get install and apt-get remove are extended regular expressions, not shell wildcards; wine* means win followed by any number of e, and since this can match any part of the package name, this means any package whose name contains win as a substring.

OP had run sudo apt-get remove wine*, ended up removing the majority of the installed packages in their system. wine+, on the other hand, would have removed packages with wine in it.

muru
  • 72,889