Regex matches unique substrings

Here is a basic regex technique that I never remembered. Let's say I use a fairly general regex implementation (e.g. grep or grep -E). If I were to make a list of files and match either end in .sty or .cls , how would I do it?

+4
source share
3 answers
 ls | grep -E "\.(sty|cls)$" 
  • \. matches literally "." - unescaped . matches any character
  • (sty|cls) - matching "sty" or "cls" - | is or , and brackets limit the expression.
  • $ causes the match to be at the end of the line

Note that you want grep -E or egrep , not grep -E as another option for template lists.

+4
source
 egrep "\.sty$|\.cls$" 
+2
source

This is a regex:
\. (STY | CBS) \ g
will match either end of line with .sty or .cls
EDIT:
for grep \ z should be replaced with $ ie. \ (Pigsty | CBS) $ as jelovirt suggested .

+2
source

Source: https://habr.com/ru/post/1276517/


All Articles