Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python regex for Java package names

Tags:

python

regex

I have problems determining valid Java package names using Python. Here's the code:

    packageName = "com.domain.lala" # valid, not rejected -> correct
    #packageName = ".com.domain.lala" # invalid, rejected -> correct
    #packageName = "com..domain.lala" # invalid, not rejected -> incorrect
    #packageName = "com.domain.lala." # invalid, not rejected -> incorrect

    matchObject = re.match("([a-z_]{1}[a-z0-9_]*(\.[a-z_]{1}[a-z0-9_]*)*)",
                           packageName)

    if matchObject is not None:
        print packageName + " is a package name!"
    else:
        print packageName + " is *not* a package name!"
        Utilities.show_error("Invalid Package Name", "Invalid package name " + packageName + "!", "Ok", "", "")

Package names must start with a lowercase letter or underscore and each dot must be followed by at least one lowercase letter or underscore again. All other characters can be lowercase letters, digits, or an underscore. No runs of dots are allowed and it may not end with or start with a dot.

How do I solve this?

like image 482
Kawu Avatar asked Aug 26 '10 16:08

Kawu


3 Answers

Add $ at the end of the regex to force matching the full string. Right now it's matching only a partial string, so it's incorrectly accepting valid package names that have garbage added at the end.

like image 141
interjay Avatar answered Oct 05 '22 18:10

interjay


Upper case letters are in fact allowed in Java package names. They are just discouraged but it works.

The regex should be:

^([a-zA-Z_]{1}[a-zA-Z0-9_]*(\\.[a-zA-Z_]{1}[a-zA-Z0-9_]*)*)?$
like image 33
Guillaume Perrot Avatar answered Oct 05 '22 18:10

Guillaume Perrot


You need to put the start of line and end of line markers. So the regex should look like -

^([a-z_]{1}[a-z0-9_]*(\.[a-z_]{1}[a-z0-9_]*)*)$
like image 36
Gopi Avatar answered Oct 05 '22 16:10

Gopi