Regex works in other engines but not Java Pattern/Matcher - java

I can't figure out why this regex doesn't work, I've tested it in php and other regex engines where it works fine and matches ",AA,".
Pattern p = Pattern.compile("(^|,)AA(,|$)");
Matcher m = p.matcher("A,B,AA,C,D");
//assigns as false
boolean matches = m.matches();
Side note: I have a split/array binary search method for doing an IN_SET / NOT_IN_SET search against the string. This is just an example I need to get working before implementing regex as another comparing option.

matches() validates the entire string. You want to use find() instead.
From the API:
matches()
Attempts to match the entire region against the pattern.
-- http://download.oracle.com/javase/6/docs/api/java/util/regex/Matcher.html#matches()
and:
find()
Attempts to find the next subsequence of the input sequence that matches the pattern.
-- http://download.oracle.com/javase/6/docs/api/java/util/regex/Matcher.html#find()

Matcher matches the entire region against the pattern. Use find().

Related

Regular Expression always returns false

I have a problem to get a regular expression to get work.
I use an XMLRPC Library to get information from an wiki.
so far so good.
After retrieving the data into a String Variable I would like to search through with a regular expression but the matcher will always return "false".
But if I asking the String ....contains("xyz"); the Answer is true.
The String looks something like this:
====== Datensicherheit ====== ''Kriterium von Sicherheit'' Typ: technisch Definition: \ //Allgemein.........
String regex = "Definition";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(text);
System.out.println(matcher.matches());
Does anybody know what I'm doing wrong?
This is an issue with your regex expression. If you are wanting to know if the string contains "Definition", your regex needs to be:
String regex = ".*Definition.*";
Note that matches() returns true if, and only if, the entire region sequence matches this matcher's pattern. see the java doc # https://docs.oracle.com/javase/7/docs/api/java/util/regex/Matcher.html#matches()
So, it will only be true if the entire "text" region matches "Definition", which is unlikely :).
Try find() instead which is true if, and only if, a subsequence of the input sequence starting at the given index matches this matcher's pattern.

Java Pattern not matching RegEx [duplicate]

This question already has answers here:
Regex doesn't work in String.matches()
(9 answers)
Closed 5 years ago.
Here is my issue. I have this:
String data = "java.awt.Color[r=168,g=228,b=160]" //example this changes.
Pattern p = Pattern.compile("(\\d+)");
Matcher m = p.matcher(data);
if(m.matches()){
....
}
However, it is not matching. Why is that? I am trying to retrieve the numbers inside brackets.
What should I try?
Matcher.matches() matches the complete string. You can use Matcher.find to match the individual integers:
while (m.find()) {
System.out.println(m.group(1));
}
Matcher.matches tells you if your regex matches the entire string. Your string isn't all digits. It contains letters, dots, equal signs, and square brackets. So you matcher doesn't match.
You want Matcher.find(). That searches for partial matches. Matcher.group then allows you to retrieve the matched portion of the input string.
Note that the Matcher.matches() method attempts to match against the entire string.
You want to use Matcher.find() instead.
The matches method will attempt to match the regex against the entire input.
Use a combination of the the find and group methods method to find and use matches within the input:
while (m.find())
System.out.println(m.group());
Because your regex doesn't match the string, there are other characters before (and after) the \d matches after all.
matches() method attempts to match the whole string, but you need just digit occurrences in it.
You need to use find() method and you might need to use while operator instead of if because it shifts matcher to next match occurrence.

Whitespace in Java's regular expression

I'm trying to write a regular expression to mach an IRC PRIVMSG string. It is something like:
:nick!name#some.host.com PRIVMSG #channel :message body
So i wrote the following code:
Pattern pattern = Pattern.compile("^:.*\\sPRIVMSG\\s#.*\\s:");
Matcher matcher = pattern.matcher(msg);
if(matcher.matches()) {
System.out.println(msg);
}
It does not work. I got no matches. When I test the regular expression using online javascript testers, I got matches.
I tried to find the reason, why it doesn't work and I found that there's something wrong with the whitespace symbol. The following pattern will give me some matches:
Pattern.compile("^:.*");
But the pattern with \s will not:
Pattern.compile("^:.*\\s");
It's confusing.
The java matches method strikes again! That method only returns true if the entire string matches the input. You didn't include anything that captures the message body after the second colon, so the entire string is not a match. It works in testers because 'normal' regex is a 'match' if any part of the input matches.
Pattern pattern = Pattern.compile("^:.*?\\sPRIVMSG\\s#.*?\\s:.*$");
Should match
If you look at the documentation for matches(), uou will notice that it is trying to match the entire string. You need to fix your regexp or use find() to iterate through the substring matches.

Matching numerical pattern

I'm doing a project which requires numerical pattern matching.
For example i want to know whether Value = 1331 is a part of 680+651 = 1331 or not, i.e. i want to match 1331 with 680+651 = 1331 or any other given string.
I'm trying pattern matching in java for the first time and i could not succeed. Below is my code snippet.
String REGEX1=s1; //s1 is '1331'
pattern = Pattern.compile(REGEX1);
matcher = pattern.matcher(line_out); //line_out is for ex. 680+651 = 1331
System.out.println("lookingAt(): "+matcher.lookingAt());
System.out.println("matches(): "+matcher.matches());
It is returning false all the times.
Pls help me.
matches() requires that the pattern be a complete match, not a partial.
You either need to change your pattern to something like .*= 1331$ or use the find() method which will do a partial match.
The matches method requires a perfect, full exact match. Since there is more text in 680+651=1331 than what is matched by the regex 1331, matches returns false.
As I pointed out in Brian's post, you need to be careful in your regex to ensure that a regex of 1331 does not match the number 213312 unless that is what you want.
matches() is the wrong method for this, use find().
http://download.oracle.com/javase/1.4.2/docs/api/java/util/regex/Matcher.html says:
public boolean matches()
Attempts to match the entire input sequence against the pattern.
and
public boolean find()
Attempts to find the next subsequence of the input sequence that matches the pattern.

Java Pattern issue

I am getting a text from the DB which contains Strings of the form
CO<sub>2</sub>
In order to recognize this I wrote the following code
String footText = "... some text containing CO<sub>2</sub>";
String co2HTML = "CO<sub>2</sub>";
Pattern pat = Pattern.compile(co2HTML);
Matcher mat = pat.matcher(footText);
final boolean hasCO2 = mat.matches();
The problem is that hasCO2 is always false although the inout text has that substring.
What is wrong hete?
Thanks!
You should use find() instead of matches(), since the latter tries to match the entire string against the pattern rather than perform a search.
From the Javadoc:
The matches method attempts to match the entire input sequence against the pattern.
The lookingAt method attempts to match the input sequence, starting at the beginning, against the pattern.
The find method scans the input sequence looking for the next subsequence that matches the pattern.
Also, the pattern in question doesn't really require regular expressions; you could use String.indexOf() to perform the search.

Categories