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?
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.
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_]*)*)?$
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_]*)*)$
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With