RegExp testing with ash shell (BusyBox)
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 '/^[a-z]{2,3}-[a-z]+[0-9]*-[0-9]+$/' na-examplename-01
Note that those match on each line inside $in
as opposed to the content of $in
as a whole. For instance, they would match on the second and third line of a $in
defined as
in='whatever
xx-a-1
yy-b-2'
As Stéphane pointed out in his answer, it's a good idea to prepend these commands with LC_ALL=C
to ensure that your locale does not confuse the character ranges.
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 https://www.gnu.org/software/findutils/manual/html_node/find_html/grep-regular-expression-syntax.html