Reputation: 585
I have a string like com.sap.fw.tr.SYSTEM.GKBN
where GKBN
is variable while rest part is fixed. I used
Pattern p = Pattern.compile("^com.sap.fw.tr.SYSTEM.([a-z]*)$");
but when I am calling Matcher.matches();
it is returning false
.
Can someone point out the mistake in this regex?
Upvotes: 0
Views: 121
Reputation: 56809
This is the most rigid solution if you want to match a valid Java identifier:
inputString.matches("\\Qcom.sap.fw.tr.SYSTEM.\\E\\p{javaJavaIdentifierStart}\\p{javaJavaIdentifierPart}*");
This uses String.matches(String regex)
instead of going through Pattern
class.
Break down of the pattern:
\Qcom.sap.fw.tr.SYSTEM.\E # Match literal string "com.sap.fw.tr.SYSTEM."
\p{javaJavaIdentifierStart} # Match 1 - starting character of a Java identifier
\p{javaJavaIdentifierPart}* # Match 0 or more - part characters of a Java identifier
This pattern makes use of java.lang.Character
classes feature, where you prepend java
in front of name of methods in java.lang.Character
classes.
The regex above checks the character based on the Character.isJavaIdentifierStart
and Character.isJavaIdentifierPart
methods.
Upvotes: 0
Reputation: 12880
You are matching an upper case input with [a-z]*
that will match lowercase letters only.
Use this
^com\\.sap\\.fw\\.tr\\.SYSTEM\\.([A-Z]*)
I have tested this in regexplanet.com. it worked! if you want to make case insensitive. You can use ?i
to ignore case matching.
Upvotes: 0
Reputation: 12306
Try to use:
("^com\\.sap\\.fw\\.tr\\.SYSTEM\\.([A-Z]+)$");
it use only uppercase letter one or more. And better escape .
symbol with \.
Upvotes: 0
Reputation: 784958
You're matching an upper case input and have [a-z]*
that will match lowercase letters only.
Try this:
Pattern p = Pattern.compile("(?i)^com\\.sap\\.fw\\.tr\\.SYSTEM\\.([a-z]*)$");
OR this:
Pattern p = Pattern.compile("^com\\.sap\\.fw\\.tr\\.SYSTEM\\.([a-z]*)$",
Pattern.CASE_INSENSITIVE);
(?i)
is used for ignore case matching
Upvotes: 2