Regex doesn't work in String.matches()
Welcome to Java's misnamed .matches()
method... It tries and matches ALL the input. Unfortunately, other languages have followed suit :(
If you want to see if the regex matches an input text, use a Pattern
, a Matcher
and the .find()
method of the matcher:
Pattern p = Pattern.compile("[a-z]");
Matcher m = p.matcher(inputstring);
if (m.find())
// match
If what you want is indeed to see if an input only has lowercase letters, you can use .matches()
, but you need to match one or more characters: append a +
to your character class, as in [a-z]+
. Or use ^[a-z]+$
and .find()
.
Java regular expression does not match
There are two issues with your code
\b
should be\\b
You should be using
find()
rather thanmatches()
. The first will do a search on a given string and stops when it finds substring that matches the regex. Second will do a search on entire string. Because the provided regex doesn't match with the full string, thematches()
does not work.
Simply fix your code on these two points, then it'll work. Tested myself.
Regular expression to match a line that doesn't contain a word
The notion that regex doesn't support inverse matching is not entirely true. You can mimic this behavior by using negative look-arounds:
^((?!hede).)*$
Non-capturing variant:
^(?:(?!:hede).)*$
The regex above will match any string, or line without a line break, not containing the (sub)string 'hede'. As mentioned, this is not something regex is "good" at (or should do), but still, it is possible.
And if you need to match line break chars as well, use the DOT-ALL modifier (the trailing s
in the following pattern):
/^((?!hede).)*$/s
or use it inline:
/(?s)^((?!hede).)*$/
(where the /.../
are the regex delimiters, i.e., not part of the pattern)
If the DOT-ALL modifier is not available, you can mimic the same behavior with the character class [\s\S]
:
/^((?!hede)[\s\S])*$/
Explanation
A string is just a list of n
characters. Before, and after each character, there's an empty string. So a list of n
characters will have n+1
empty strings. Consider the string "ABhedeCD"
:
┌──┬───┬──┬───┬──┬───┬──┬───┬──┬───┬──┬───┬──┬───┬──┬───┬──┐
S = │e1│ A │e2│ B │e3│ h │e4│ e │e5│ d │e6│ e │e7│ C │e8│ D │e9│
└──┴───┴──┴───┴──┴───┴──┴───┴──┴───┴──┴───┴──┴───┴──┴───┴──┘
index 0 1 2 3 4 5 6 7
where the e
's are the empty strings. The regex (?!hede).
looks ahead to see if there's no substring "hede"
to be seen, and if that is the case (so something else is seen), then the .
(dot) will match any character except a line break. Look-arounds are also called zero-width-assertions because they don't consume any characters. They only assert/validate something.
So, in my example, every empty string is first validated to see if there's no "hede"
up ahead, before a character is consumed by the .
(dot). The regex (?!hede).
will do that only once, so it is wrapped in a group, and repeated zero or more times: ((?!hede).)*
. Finally, the start- and end-of-input are anchored to make sure the entire input is consumed: ^((?!hede).)*$
As you can see, the input "ABhedeCD"
will fail because on e3
, the regex (?!hede)
fails (there is "hede"
up ahead!).
String.matches() doesn't apply regex
You need a case insensitive pattern or modify your regex to support lower case letters. To get a case insensitive Pattern use:
String email = "mail-q7hplbmer93rhtzxyd85-4m3fscngz5c9xwzy63db-event@mydomain.com";
Pattern pattern = Pattern.compile("mail-[A-Z0-9]+-[A-Z0-9]+-event@mydomain.com", Pattern.CASE_INSENSITIVE);
System.out.println(pattern.matcher(email).matches());
Output:
true
regex to match the end of string doesn't work
If I got your problem correctly, this should work:
(\d+(\s?[a-zA-Z]?\s?|\s?[a-zA-Z]$))
Note: [\s]{0,1}
is the same as \s?
https://regex101.com/r/r6WHFy/1
The issue in your regex was that The house number is 23 a
matches ([\s]{0,1}[a-zA-Z]{0,1}[\s])*
part, thus the parser "does not need" to look for the part with end of string symbol.
String.matches() returning false
Regex is not globbing!
Your regex "H*I*Y"
does not mean "H
then anything then I
then anything then Y"; it means "any number of H
(including none) followed by any number of I
(including none) followed by a Y
".
The regex equivalent of globbing's *
is .*
: the dot means "any character" an *
means "any number of (including none)".
Try:
String pattern = "H.*I.*Y";
method matches not work well
In Java, matches
attempts to match a pattern against the entire string.
This is true for String.matches
, Pattern.matches
and Matcher.matches
.
If you want to check if there's a match somewhere in a string, you can use .*\bi.*
. In this case, as a Java string literal, it's ".*\\bi.*"
.
java.util.regex.Matcher
API links
boolean matches()
: Attempts to match the entire region against the pattern.
What .*
means
As used here, the dot .
is a regex metacharacter that means (almost) any character. *
is a regex metacharacter that means "zero-or-more repetition of". So for example something like A.*B
matches A
, followed by zero-or-more of "any" character, followed by B
(see on rubular.com).
References
- regular-expressions.info/Repetition with Star and Plus and The Dot Matches (Almost) Any Character
Related questions
- Difference between
.*?
and.*
for regex
Note that both the .
and *
(as well as other metacharacters) may lose their special meaning depending on where they appear. [.*]
is a character class that matches either a literal period .
or a literal asterisk *
. Preceded by a backslash also escapes metacharacters, so a\.b
matches "a.b"
.
- regular-expressions.info/Character Class and Literal Characters and Metacharacters
Related problems
Java does not have regex-based endsWith
, startsWith
, and contains
. You can still use matches
to accomplish the same things as follows:
matches(".*pattern.*")
- does it contain a match of the pattern anywhere?matches("pattern.*")
- does it start with a match of the pattern?matches(".*pattern")
- does it end with a match of the pattern?
String
API quick cheat sheet
Here's a quick cheat sheet that lists which methods are regex-based and which aren't:
- Non-regex methods:
String replace(char oldChar, char newChar)
String replace(CharSequence target, CharSequence replacement)
boolean startsWith(String prefix)
boolean endsWith(String suffix)
boolean contains(CharSequence s)
- Regex methods:
String replaceAll(String regex, String replacement)
String replaceFirst(String regex, String replacement)
String[] split(String regex)
boolean matches(String regex)
java pattern matches not working
You need to call find()
to make the engine find its match before trying to access it.
String s = "public class hello extends jframe";
Pattern p = Pattern.compile("public\\s*class\\s+(\\S+)");
Matcher m = p.matcher(s);
if (m.find()) {
System.out.println("found");
String className = m.group(1);
System.out.println(className);
}
Ideone Demo
Related Topics
Conditionally Ignoring Tests in Junit 4
How to Read and Write Xml Files
Java - Method Name Collision in Interface Implementation
Java Code for Calculating Leap Year
Powermockito Mock Single Static Method and Return Object
String's Maximum Length in Java - Calling Length() Method
Why Java Needs Serializable Interface
Preferred Way of Loading Resources in Java
How to Print Binary Tree Diagram in Java
Obtaining a Powerset of a Set in Java
How Will Java Lambda Functions Be Compiled
How to Map an Entity Field Whose Name Is a Reserved Word in JPA
Java Securityexception: Signer Information Does Not Match
Random Weighted Selection in Java
How Does Cloneable Work in Java and How to Use It
.Toarray(New Myclass[0]) or .Toarray(New Myclass[Mylist.Size()])