79640950

Date: 2025-05-27 17:33:38
Score: 0.5
Natty:
Report link

To expand further on the answers above, when wanting to match files with regular patterns, it's possible to embed patterns within patterns.

The command does start looking a bit of a mess thanks to the escapes needed to ensure that the brackets are interpreted correctly.

Example: A find command to match files called .cpp, *.hpp, *.cxx, *.hxx

find . -type f -regex '.*\.\([hc]\(pp\|xx\)\)$' 

When we omit the required escapes for the brackets and pipes for clarity, it's a little easier to see what's going on:

find . -type f -regex '.*\.([hc](pp|xx))$' | grep cxx

Breakdown:


.*\.
# Match any char, any number of times, when it's followed by a dot

(
# Multi-char expression open

[hc]
# First letter after the dot is h or c

(pp|xx)
# Multi-char2 - the next two chars to match are pp or xx

)
# Multi-char1 close

$
# Match the next char after pp/xx when end of line
Reasons:
  • Blacklisted phrase (0.5): thanks
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: Shewbs