I’ve got the following code:
public class testMatch {
public static void main(String[] args) {
String dummyMessage = "asdfasdfsadfsadfasdf 3 sdfasdfasdfasdf";
String expression = "3";
if (dummyMessage.matches(expression)){
System.out.println("MATCH!");
} else {
System.out.println("NO MATCH!");
}
}
}
I’d expect this to be a successful match as the dummyMessage contains the expression 3 but when I run this snippet the code prints NO MATCH!
I don’t get what I’m doing wrong.
OKAY STOP ANSWERING! .*3.* works
This is an over simplification of an issue I have in some live code, the regex is configurable, and up until now matching the entire string has been okay, I’ve now had to match a part of the string and was wondering why it wasn’t working.
It matches against the whole string, i.e. like
^3$in most other regex implementations. So3does not match e.g.333or your string. But.*3.*would do the job.However, if you just want to test if “3” is contained in your string you don’t need a regex at all. Use
dummyMessage.contains(expression)instead.