Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to match unique substrings

Tags:

regex

grep

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?

like image 998
Will Robertson Avatar asked Mar 01 '23 08:03

Will Robertson


1 Answers

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.

like image 173
Dave Webb Avatar answered Mar 05 '23 19:03

Dave Webb