I have a line of Java code
System.out.println("...Somtime".matches("^[^a-zA-Z]"));
Which returns false. Why? Can any one help?
String#matches matches at both the ends, so your pattern should cover the complete string. And also you don't need to give those anchors (Caret - ^) at the beginning. It is implicit.
Now, since your first three characters matches - [^a-zA-Z], while the later characters matches - [a-zA-Z].
So, probably you want: -
"...Somtime".matches("[^a-zA-Z]{3}[a-zA-Z]+")
String.matches("regex")
This method will match the regex against the WHOLE string. If the string matches regex, it will return true and false otherwise
System.out.println("...Somtime".matches("^[^a-zA-Z]{3}[a-zA-Z]+"));
here for three dots you are using {3} and this return true
System.out.println("Somtime".matches("^[^a-zA-Z]"));
it return false
Related
So "XXXXX**".matches("[X{9,11}\\*{2,3}]") returns false as expected...
But, "XXXXX**".matches("[X{9,11}\\*{2,3}]+") returns true. Am I using the + quantifier correctly? (I want the second one to also return false)
[...] matches any character defined in the character class, so
[X{9,11}\\*{2,3}] actually means, a single character which is: X, or open brace, or 9, or comma, or 1, or 1 (yes you have it duplicated), or backslash, or asterisk....
So as your string have more than character in your string to-be-matched, such pattern will not match.
When you add a +, it means matching a string with 1 or more [ X or asterisk or....], so it match
I believe what you really want to do is using a group.
So the regex looks like (X{9,11}\*{2,3}])+
"XXXXXXXXX**".matches("(X{9,11}\\*{2,3})+")
"XXXXXXXX**".matches("(X{9,11}\\*{2,3})+")
match true and false.
The whole thing of (Xes and *s) has to occur at least once (+).
No group of characters involved, no need to use '[]'.
The following piece of code returns false when I believe it should return true. Can anyone tell me why?
It's using java.util.regex.Pattern to parse the regex.
Pattern.compile("^\|:\|$".matcher("| |").matches();
A \ in a string literal must be escaped as \\.
If you want to match anything in between, use .*, not :.
When you use the matches method in Java, you don't need to add the string boundaries ^ and $ as this function returns true only when it matches the whole string.
You seem to want
Pattern.compile("\\|.*\\|").matcher("| |").matches();
if("test%$#*)$(%".matches("[^a-zA-Z\\.]"))
System.exit(0);
if("te/st.txt".matches("[^a-zA-Z\\.]"))
System.exit(0);
The program isn't exiting even though the regexes should be returning true. What's wrong with the code?
matches returns true only if regex matches entire string.
In your case your regex represents only one character that is not a-z, A-Z or ..
I suspect that you want to check if string contains one of these special characters which you described in regex. In that case surround your regex with .* to let regex match entire string. Oh, and you don't have to escape . inside character class [.].
if ("test%$#*)$(%".matches(".*[^a-zA-Z.].*")) {
//string contains character that is not in rage a-z, A-Z, or '.'
BUT if you care about performance you can use Matcher#find() method which
can return true the moment it will find substring containing match for regex. This way application will not need to check rest of the text, which saves us more time the longer remaining text is.
Will not force us to constantly build Pattern object each time String#matches(regex) is called, because we can create Pattern once and reuse it with different data.
Demo:
Pattern p = Pattern.compile("[^a-zA-Z\\.]");
Matcher m = p.matcher("test%$#*)$(%");
if(m.find())
System.exit(0);
//OR with Matcher inlined since we don't really need that variable
if (p.matcher("test%$#*)$(%").find())
System.exit(0);
x.matches(y) is equivalent to
Pattern.compile(y).matcher(x).matches()
and requires the whole string x to match the regex y. If you just want to know if there is some substring of x that matches y then you need to use find() instead of matches():
if(Pattern.compile("[^a-zA-Z.]").matcher("test%$#*)$(%").find())
System.exit(0);
Alternatively you could reverse the sense of the test:
if(!"test%$#*)$(%".matches("[a-zA-Z.]*"))
by providing a pattern that matches the strings that are allowed rather than the characters that aren't, and then seeing whether the test string fails to match this pattern.
You obtain always false because the matches() method returns true only when the pattern matches the full string.
What would be a regular expression that would evaluate to true if the string has one or more letters anywhere in it.
For example:
1222a3999 would be true
a222aZaa would be true
aaaAaaaa would be true
but:
1111112())-- would be false
I tried: ^[a-zA-Z]+$ and [a-zA-Z]+ but neither work when there are any numbers and other characters in the string.
.*[a-zA-Z].*
The above means one letter, and before/after it - anything is fine.
In java:
String regex = ".*[a-zA-Z].*";
System.out.println("1222a3999".matches(regex));
System.out.println("a222aZaa ".matches(regex));
System.out.println("aaaAaaaa ".matches(regex));
System.out.println("1111112())-- ".matches(regex));
Will provide:
true
true
true
false
as expected
^.*[a-zA-Z].*$
Depending on the implementation, match() functions check if the entire string matches (which is probably why your [a-zA-Z] or [a-zA-Z]+ patterns didn't work).
Either use match() with the above pattern or use some sort of search() method instead.
This regexp should do it:
[a-zA-Z]
It matches as long as there's a single letter anywhere in the string, it doesn't care about any of the other characters.
[a-zA-Z]+
should have worked as well, I don't know why it didn't for you.
.*[a-zA-Z]?.*
Should get you the result you want.
The period matches any character except new line, the asterisk says this should exist zero or more times. Then the pattern [a-zA-Z]? says give me at least one character that is in the brackets because of the use of the question mark. Finally the ending .* says that the alphabet characters can be followed by zero or more characters of any type.
if("test%$#*)$(%".matches("[^a-zA-Z\\.]"))
System.exit(0);
if("te/st.txt".matches("[^a-zA-Z\\.]"))
System.exit(0);
The program isn't exiting even though the regexes should be returning true. What's wrong with the code?
matches returns true only if regex matches entire string.
In your case your regex represents only one character that is not a-z, A-Z or ..
I suspect that you want to check if string contains one of these special characters which you described in regex. In that case surround your regex with .* to let regex match entire string. Oh, and you don't have to escape . inside character class [.].
if ("test%$#*)$(%".matches(".*[^a-zA-Z.].*")) {
//string contains character that is not in rage a-z, A-Z, or '.'
BUT if you care about performance you can use Matcher#find() method which
can return true the moment it will find substring containing match for regex. This way application will not need to check rest of the text, which saves us more time the longer remaining text is.
Will not force us to constantly build Pattern object each time String#matches(regex) is called, because we can create Pattern once and reuse it with different data.
Demo:
Pattern p = Pattern.compile("[^a-zA-Z\\.]");
Matcher m = p.matcher("test%$#*)$(%");
if(m.find())
System.exit(0);
//OR with Matcher inlined since we don't really need that variable
if (p.matcher("test%$#*)$(%").find())
System.exit(0);
x.matches(y) is equivalent to
Pattern.compile(y).matcher(x).matches()
and requires the whole string x to match the regex y. If you just want to know if there is some substring of x that matches y then you need to use find() instead of matches():
if(Pattern.compile("[^a-zA-Z.]").matcher("test%$#*)$(%").find())
System.exit(0);
Alternatively you could reverse the sense of the test:
if(!"test%$#*)$(%".matches("[a-zA-Z.]*"))
by providing a pattern that matches the strings that are allowed rather than the characters that aren't, and then seeing whether the test string fails to match this pattern.
You obtain always false because the matches() method returns true only when the pattern matches the full string.