Tag Info

New answers tagged


You have three tools that can do regular expressions. These all assume that $in contains na-examplename-01. grep $ printf "%s\n" "$in" | ./grep -E '^[a-z]{2,3}-[a-z]+[0-9]*-[0-9]+$' na-examplename-01 sed $ printf "%s\n" "$in" | ./sed -n '/^[a-z]\{2,3\}-[a-z]\+[0-9]*-[0-9]\+$/p' na-examplename-01 awk $ printf "%s\n" "$in" | ./awk ...


awk sounds like a good candidate: input='whatever even spaces and newlines xxx-blah12-0' # should not match input='na-examplename-01' # should match if LC_ALL=C awk 'BEGIN{exit(!(ARGV[1] ~ /^[a-z]{2,3}-[a-z]+[0-9]*-[0-9]+$/))}' "$input" then echo it matches else echo >&2 it does not match fi


You could use grep in extended regex mode like this: echo na-examplename-01 | grep -E '^[a-z]{2,3}-[a-z]+[0-9]*-[0-9]+$' You should use the interval parameter to make this more easy to read. [a-z][a-z]|[a-z][a-z][a-z] would be [a-z]{2,3}. [a-z]+ is the same as [a-z][a-z]* For the grep snytax, take a look at ...

Top 50 recent answers are included