Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

`{n}` iteration meta character after posix class in regex

Tags:

regex

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?

like image 747
mate_eu Avatar asked May 14 '14 06:05

mate_eu


People also ask

What is POSIX in regex?

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.

What are meta characters in regex?

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.

What does \\ mean in regex?

\\. 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?\ ' .


1 Answers

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 \|).

like image 107
Konrad Rudolph Avatar answered Dec 13 '22 23:12

Konrad Rudolph