Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matching numerical pattern

Tags:

java

regex

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.

like image 372
Anil Avatar asked May 25 '26 17:05

Anil


1 Answers

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.

like image 181
Brian Roach Avatar answered May 27 '26 06:05

Brian Roach



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!