You are not logged in.
I need to match the following four patterns of a filename (a,b,c,d are arbitrary characters; . X and ext are literal characters) in a Bash script, and here's the regex which works for me:
file='ab.'
file='ab.Xcd'
file='ab.ext'
file='ab.extXcd'
[[ $file =~ (\.$|\.X|\.ext$|\.extX) ]] && echo matches
What I wonder is whether this can be stated more succinctly using () grouping and a repetition operator such as * ? +
I tried a few possibilities but the only expression I could get working right was the one above.
Any help gratefully received!
Last edited by ninian (2014-05-11 20:00:37)
Offline
Well, I wouldn't use regex here.
[[ $file = *.?(ext)?(X??) ]] && echo "$file matches"
Aha - thank you very much - seems to do the trick, and more understandable too.
Offline