0

I have a bunch of files from which I need to read parts of the filename. Therefore I thought a great use for regex and grouping :D the file could be named kb123.pdf or KB123.pdf so there is always kb or KB in the beginning and then 3 digits followed by .pdf I have the following code, but get "OH NO"s

Pattern pattern = Pattern.compile("^kb|KB(\\d{3})\\.pdf"); Matcher matcher = pattern.matcher("kb165.pdf"); if(matcher.matches()) { System.out.println("YAY"); }else { System.out.println("OH NO"); } 

can anyone please explain, why the regex from the debugger in correct format ("\" got transformed in the debugger to the resulting "") as it shows to me:

^kb|KB(\d{3})\.pdf 

works on regex101, but not in java? https://regex101.com/r/uRHkd0/1

3
  • 2
    Try "^(kb|KB)(\\d{3})\\.pdf" because regex concatenation (ab) binds stronger than regex alternation (a|b). Commented Apr 22 at 12:46
  • Your regex101 example works, but not as you expect. See this one, with another example input: regex101.com/r/ohsZo6/1 Commented Apr 22 at 12:51
  • 1
    by the way, no need to use ^ (or $) if using matches(), since it will only match against the whole input string anyway Commented Apr 22 at 12:52

1 Answer 1

3

You need to put your OR logic in brackets, best to use a non capturing group ?:

^(?:kb|KB)\d{3}\.pdf

Another option is to ignore case

(?i)^kb\d{3}\.pdf

Sign up to request clarification or add additional context in comments.

1 Comment

but be aware that ignoring case will also match kB or Kb (maybe even desired...)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.