I am learning regular expressions. I have some doubts relating to that example :
I'd like to find all the files with conventional extensions in names. This works:
ls | grep '\.[[:lower:]][[:lower:]][[:lower:]]$'
but this does not:
ls | grep '\.[[:lower:]]{3}$'
As far as I understood the {n}
iteration meta character results in matching with pattern that has exactly {n}
occurrences of preceding character. Doesn't it work with POSIX classes? Or am I making some silly mistake here?
POSIX bracket expressions are a special kind of character classes. POSIX bracket expressions match one character out of a set of characters, just like regular character classes. They use the same syntax with square brackets.
A metacharacter is a character that has a special meaning during pattern processing. You use metacharacters in regular expressions to define the search criteria and any text manipulations. Search string metacharacters are different from replacement string metacharacters.
\\. matches the literal character . . the first backslash is interpreted as an escape character by the Emacs string reader, which combined with the second backslash, inserts a literal backslash character into the string being read. the regular expression engine receives the string \. html?\ ' .
In POSIX basic (BRE), you need to escape the braces for this to work:
ls | grep '\.[[:lower:]]\{3\}$'
In POSIX extended (ERE), this requirement has been dropped, and ERE also finally adds alternation (|
) to regular expressions (although some tools which use BRE do support alternation via \|
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With