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
"^(kb|KB)(\\d{3})\\.pdf"because regex concatenation (ab) binds stronger than regex alternation (a|b).^(or$) if usingmatches(), since it will only match against the whole input string anyway