Regexp to match java package name

I want to check if arguments are passed to stdin to make sure they match a valid java package name. The regular expression is not working correctly for me. With the following code going through com.example.package I get an error. I'm not sure what is wrong with my regex?

 17         if ! [[ $1 =~ $regex ]]; then
 18                 >&2 echo "ERROR: invalid package name arg 1: $1"
 19                 exit 2
 20         fi



source to share

2 answers

You are pretty close to making the right decision. Just tweak the regex a bit (also consider the simplified regex @fede) and set the parameter nocasematch

to case insensitive. For example:


shopt -s nocasematch
if ! [[ $1 =~ $regex ]]; then
  exit 2
shopt -u nocasematch


You are probably being misled by other languages ​​that use /regex/i

(javascript) or qr/regex/i

(perl) to define a case insensitive regex object.

Btw, use grep -qi

is a different, more portable solution. Greetings.



You can use a simpler regex:



Working demo



All Articles