Python regular expression for java package name
•
Java
I have a problem using Python to determine a valid java package name This is 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","","")
The package name must start with a lowercase letter or underscore, and each point must be followed by at least one lowercase letter or underscore All other characters can be lowercase letters, numbers or underscores Run point is not allowed, it may not end or start with a point
How can I solve this problem?
Solution
Add $to the end of the regular expression to force a full string match Now it only matches a partial string, so it mistakenly accepts a valid package name with the last garbage added
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码