java - Check if result is unique -
I want to draw a number 3-5 length from the text The pattern below works for all my cases.
Pattern Pattern = Pattern.compile ("(^ | [\\ D] |. * [\\ D]). (? & Lt; number & gt; [0-9] {3,5}) ($ | | [\\ D] | [\\ D] *) "). // Example of working always returns 111 / Matcher m = pattern.matcher ("XX 111, YYY 371240 9 1"); // Matcher m = pattern.matcher ("XX111"); // Matcher m = pattern.matcher ("X111"); // Matcher m = pattern.matcher ("111"); I have only one problem when the pattern, as I believe, should find many matches according to the example below (111, 123), instead of just 123
Matcher m = pattern.matcher ("XX 111, CCC 123 YYY 37124091"); While (m.find ()) {System.out.println (m.group ("number")}} How can I identify that the pattern matches 2 times, or Why does it not match for 111 and 123?
The only problem I get when patterns,
You need to relieve the * quantifers instead, because I believe, many matches should be found according to the examples given below (111, 123)
XX 111, CCC 123 YYY 37124091 . * \\ D part "XX 111, CCC" ^ --------- ^^ | | * * * \\ D Consumer 111 . Try changing
Pattern .compile ("(^ | \\ D |. * \\ D) (? & Lt; number & gt; [0- 9] {3,5}) ($ | \\ D | \\ D. *) "); to
Pattern.compile (" (^ | \\ D | * \\ D) (lt and; number> gt; ; [0-9] {3,5}) ($ | \\ D | \ \ D. *?) "); // ^ ^ In addition to \\ D this is the LF character class so that you get it from [ ..] In other words, you do not need to write it as [\\ D] , simple \\ D is enough
How can I identify that the pattern matches 2 times
Reggae all the previously matched results Will not miss if you want to get only unique values Keep them in the set.
Comments
Post a Comment