4

Here's a basic regex technique that I've never managed to remember. Let's say I'm using a fairly generic regex implementation (e.g., grep or grep -E). If I were to do a list of files and match any that end in either .sty or .cls, how would I do that?

eLRuLL
  • 18,488
  • 9
  • 73
  • 99
Will Robertson
  • 62,540
  • 32
  • 99
  • 117

3 Answers3

4
ls | grep -E "\.(sty|cls)$"
  • \. matches literally a "." - an unescaped . matches any character
  • (sty|cls) - match "sty" or "cls" - the | is an or and the brackets limit the expression.
  • $ forces the match to be at the end of the line

Note, you want grep -E or egrep, not grep -e as that's a different option for lists of patterns.

David Webb
  • 190,537
  • 57
  • 313
  • 299
2
egrep "\.sty$|\.cls$"
Peter Hoffmann
  • 56,376
  • 15
  • 76
  • 59
2

This regex:
\.(sty|cls)\z
will match any string ends with .sty or .cls
EDIT:
for grep \z should be replaced with $ i.e. \.(sty|cls)$ as jelovirt suggested.

Community
  • 1
  • 1
aku
  • 122,288
  • 32
  • 173
  • 203