I’ve checked every single post that I can find on here, but can’t figure this out.
I have a string which looks something like this: “ABC1234598901AC”
I’m trying to use a Regular Expression to match “5989” within the string, but I want to be able to match that string even if one of the characters is something different.
To simplify, let’s say I am searching the string for “59(Random Character that’s not 8)9”.
Right now here is my Regular Expression: “59[^8]9” but when I use the Matcher in Java it is not matching at all.
Here’s the code I’m using to test this:
Matcher test = Pattern.compile("59[^8]9").matcher("ABC1234598901AC");
if (test.matches())
{
System.out.println(test.start());
System.out.println(test.end());
}
Test.matches() is never evaluating to true.
Any help is appreciated, thanks!
You want to use
test.find(), nottest.matches().Matcher.matches()requires your pattern to match the entire input (thus you would need to surround the pattern with.*for it to match), whereasMatcher.find()searches the input string for the first (and then subsequent) substring that matches your pattern.