tags:

views:

27

answers:

3

Hello,

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?

+1  A: 

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.

interjay
So close... and so fast. THANKS!
Kawu
A: 

You can parse the string instead:

def valid_java_package_name(string):
    tree = string.split('.')

    if len(tree) == 0:
        return false

    for node in tree:
        if not valid_java_package_node(node):
            return false

    return true
Beau Martínez
+1  A: 

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_]*)*)$
Gopi
You don't need the start marker when using `re.match`.
interjay
@@interjay ohh.. I am not a python guy. This is general regex.
Gopi