[SOLVED] regexp to match java package name


I want to check if arguments passed in stdin to see if they conform to a valid java package name. The regex I have is not working properly. With the following code passing in com.example.package I receive the error message. 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


You are pretty close to the correct solution. Just tweak the regex a bit (also consider @fede’s simpler regex) and set the nocasematch option for case insensitive matching. 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) for defining a case-insensitive regex object.

Btw, Using grep -qi is another, more portable, solution. Cheers.

Answered By – gvalkov

Answer Checked By – Cary Denson (BugsFixing Admin)

Leave a Reply

Your email address will not be published.