Match optional pattern with find command

M0M0

I want to match all files with a typical fortran file extension with find. Typical extensions are .f .F .for .FOR .f90 .F90 .f95 .F95 .f03 .F03 .f08 .F08 .f15 .F15 .f18 .F18

what I tried so far is find * -name "*\.[Ff][0-9Oo][0-9Rr]" this works for all the mentioned extensions despite .f and .F. For that I have to make [0-9Oo] and [0-9Rr] optional which is usually done with "?". However, this find * -name "*\.[Ff][0-9Oo]?[0-9Rr]?" does not work with my version of find.

Wiktor Stribiżew

With a GNU find, you can use

find * -regextype posix-extended -regex '.*\.[Ff][0-9Oo]?[0-9Rr]?'

The pattern matches a string that fully matches:

  • .*\. - any text up to the rightmost . including it
  • [Ff] - F or f
  • [0-9Oo]? - an optional digit, O or o
  • [0-9Rr]? - an optional digit, R or r.

You can also use a non-regex approach using mere glob patterns:

find * ( -name "*.[Ff][0-9oO][0-9rR]" -o -name "*.[Ff]" )

Note that here, the * glob pattern matches any text. In the previous solution, it was a quantifier, an operator that makes the preceding pattern match zero or more times. Also, in glob patterns, . matches a literal dot, no need to escape it.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

match optional pattern

Match the first occurrence of an optional pattern

Makefile match pattern in command

Grok pattern to match URIPATH with optional URIPARAM

find command regex with optional subexpression

sed command to match a pattern and replace it

find command match issue

find a Pattern Match in string in Python

Find Files that Match a Dynamic Pattern

find match pattern in string object

Find files that match the regex pattern

Pattern match and find another pattern in the next line

Bash command `find` with Regex pattern doesn't match correctly non-word-characters

Find pattern match 2 occurring just before pattern match 1

find command with 'regex' match not working

what's the right pattern to match my command

Cypher multiple OPTIONAL MATCH - Pattern Comprehension - COUNT DISTINCT

Neo4j Cypher RETURN with OPTIONAL MATCH in pattern comprehensions

Neo4j Cypher pattern comprehension with optional match

how to use any character match followed by an optional matching pattern?

Regex match pattern with optional character and min/max length

Parse url with regexp, pattern doesnt match optional string

Javascript regex to match between two patterns, where first pattern is optional

Find all strings which match the pattern

Find the shortest match between two occurrences of a pattern

Find all records that match a pattern in collection

Linux find and grep for files not match pattern

pattern to match multiple filenames with find utility

Find sequences in vector that do not match a pattern