1

I need a regex that ignores the occurence of certain word inside the input.

The regex works in a normal online regex tester but when I put it in java it doesn't match anymore

input string IGNORE 23 55 21 66 IGNORE 55

Regex: ((IGNORE)*)(\d{2})(.*)

  final Pattern p = Pattern.compile( "((IGNORE)*)(\\d{2})(.*)" );

  final Matcher m = p.matcher( "IGNORE 23 55 21 66 IGNORE 55" );

  final boolean b = m.matches();

  System.out.println( "matches = " + b );

The output is false

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
Mary2349
  • 33
  • 4
  • Where did you try this regex? and what's the expected output (as in the information you want to retrieve, or do you just want to test if it matches)? – Haytam Aug 28 '19 at 08:36
  • **Use `m.find()`.** – Wiktor Stribiżew Aug 28 '19 at 08:43
  • Agreed with comment above - you can simply check string using .contains method and then parse it via the regex. – Ruslan K. Aug 28 '19 at 08:43
  • If you just want to extract part of a string after `IGNORE` word, you are doing it wrong, use: `final Pattern p = Pattern.compile( "IGNORE\\s*(\\d{2})" );` and then `if (m.find()) { System.out.println( "matched string = " + m.group(1); }` – Wiktor Stribiżew Aug 28 '19 at 08:54

0 Answers0