1

I am trying to match a string using java regex.

My string is "abc.txt" which is a file name. The regex I am using is "abc".

String fileName = "abc.txt";
Pattern pattern = Pattern.compile("abc");
Matcher matcher = pattern.matcher(fileName);
System.out.println(matcher.matches());

But some how it does not match. Infact using notepad++ regex search, it matches completely fine.

Please help

VLAZ
  • 26,331
  • 9
  • 49
  • 67
DockYard
  • 989
  • 2
  • 12
  • 29
  • 3
    `matches()` looks for complete matches and not just for partial matches. Unless your regex matches the whole string, it wont match. In you case, a proper fileName would be `abc` or the proper regex `abc\\.txt` – XtremeBaumer Jun 14 '19 at 13:09
  • 1
    try abc.* ? In fact it depends what strings you want to match in the end – Aloïs de La Comble Jun 14 '19 at 13:09

0 Answers0