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.
Related
This question already has answers here:
Difference between matches() and find() in Java Regex
(5 answers)
Closed 3 years ago.
I am using regex in java, and I cannot create a regex to match what I want it to. I want to match everything in a string that begins and ends with a character.
"cats-are-cute" should match and return cats-are-cute
!!!DOG-CAT!!! should match and return DOG-CAT
I am using https://regexr.com/ to test, and it says my regex should work
I'm not even sure how I should attempt to fix this. I've found out that it will quite if the very first character does not match (e.i it is a special character) but it will match if the entire string begins + ends with a matching character.
It will not match if a special character begins or ends the entire string
Here is my code:
Pattern pattern = Pattern.compile("([A-Za-z0-9].*[A-Za-z0-9])");
Matcher matcher = pattern.matcher(word);
if(matcher.matches())
{
System.out.println("Matches");
System.out.println(matcher.start());
System.out.println(matcher.end());
}
if I type
testing
it returns
Matches
0
7
Small question: why is it 7 and not 6?
just like it should
but if I do "testing" matcher.matches() is false.
I think it should output
Matches
1
7
but sadly it does not as matcher.matches() returns false.
I think my regex is working, because quite a few sites have said that my regex will match what I want it to.
Am I missing something with Matcher matches()? Does it not do what I think it does?
I just needed to use find instead of matches, as OH GOD SPIDERS suggested in this comment:
As the documentation of Matcher.matches states it Attempts to match the entire region against the pattern.. You need to use Matcher.find if you don't want your entire String to be matched.
Is there a regular expression that will capture all instances of an expression, regardless of whether or not they overlap?
E.g. in /abc/def/ghi if I want to capture all strings beginning with /. The regex (/.*) only returns the entire string, but I'd want it to match on /def/ghi and /ghi as well.
Sure, match an empty string and place a look-ahead after it that captures /.* in a capturing group:
Matcher m = Pattern.compile("(?=(/.*))").matcher("/abc/def/ghi");
while(m.find()) {
System.out.println(m.group(1));
}
would print:
/abc/def/ghi
/def/ghi
/ghi
Possible duplicate: Print regex matches in java
I am using Matcher class in java to match a string with a particular regular expression which I converted into a Pattern using the Pattern class. I know my regex works because when I do Matcher.find(), I am getting true values where I am supposed to. But I want to print out the stings that are producing those true values (meaning print out the strings that match my regex) and I don't see a method in the matcher class to achieve that. Please do let me know if anyone has encountered such a problem before. I apologize as this question is fairly rudimentary but I am fairly new to regex and hence am still finding my way around the regex world.
Assuming mis your matcher:
m.group() will return the matched string.
[EDIT] Added info regarding matched groups
Also, if your regex has portions inside parenthesis, m.group(n) will return the string that matches the nth group inside parenthesis;
Pattern p = Pattern.compile("mary (.*) bob");
Matcher m = p.matcher("since that day mary loves bob");
m.group() returns "mary loves bob".
m.group(1) return "loves".
Consider the following code snippet:
String input = "Print this";
System.out.println(input.matches("\\bthis\\b"));
Output
false
What could be possibly wrong with this approach? If it is wrong, then what is the right solution to find the exact word match?
PS: I have found a variety of similar questions here but none of them provide the solution I am looking for.
Thanks in advance.
When you use the matches() method, it is trying to match the entire input. In your example, the input "Print this" doesn't match the pattern because the word "Print" isn't matched.
So you need to add something to the regex to match the initial part of the string, e.g.
.*\\bthis\\b
And if you want to allow extra text at the end of the line too:
.*\\bthis\\b.*
Alternatively, use a Matcher object and use Matcher.find() to find matches within the input string:
Pattern p = Pattern.compile("\\bthis\\b");
Matcher m = p.matcher("Print this");
m.find();
System.out.println(m.group());
Output:
this
If you want to find multiple matches in a line, you can call find() and group() repeatedly to extract them all.
Full example method for matcher:
public static String REGEX_FIND_WORD="(?i).*?\\b%s\\b.*?";
public static boolean containsWord(String text, String word) {
String regex=String.format(REGEX_FIND_WORD, Pattern.quote(word));
return text.matches(regex);
}
Explain:
(?i) - ignorecase
.*? - allow (optionally) any characters before
\b - word boundary
%s - variable to be changed by String.format (quoted to avoid regex
errors)
\b - word boundary
.*? - allow (optionally) any characters after
For a good explanation, see: http://www.regular-expressions.info/java.html
myString.matches("regex") returns true or false depending whether the
string can be matched entirely by the regular expression. It is
important to remember that String.matches() only returns true if the
entire string can be matched. In other words: "regex" is applied as if
you had written "^regex$" with start and end of string anchors. This
is different from most other regex libraries, where the "quick match
test" method returns true if the regex can be matched anywhere in the
string. If myString is abc then myString.matches("bc") returns false.
bc matches abc, but ^bc$ (which is really being used here) does not.
This writes "true":
String input = "Print this";
System.out.println(input.matches(".*\\bthis\\b"));
You may use groups to find the exact word. Regex API specifies groups by parentheses. For example:
A(B(C))D
This statement consists of three groups, which are indexed from 0.
0th group - ABCD
1st group - BC
2nd group - C
So if you need to find some specific word, you may use two methods in Matcher class such as: find() to find statement specified by regex, and then get a String object specified by its group number:
String statement = "Hello, my beautiful world";
Pattern pattern = Pattern.compile("Hello, my (\\w+).*");
Matcher m = pattern.matcher(statement);
m.find();
System.out.println(m.group(1));
The above code result will be "beautiful"
Is your searchString going to be regular expression? if not simply use String.contains(CharSequence s)
System.out.println(input.matches(".*\\bthis$"));
Also works. Here the .* matches anything before the space and then this is matched to be word in the end.
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().