Home > Back-end >  Java Regex pattern not working for filename matching
Java Regex pattern not working for filename matching

Time:11-30

I am trying to pull filenames with the below pattern RID12345678_DLF0013B-P-21311118121541.txt RID123456789_DLF0013B-P-21311118121541.txt RID12345678_DLF0013B-I-21311118121541.txt

So below is the Regex pattern I gave

"([\w{11,12}]_DLF0013B-[I|P]-[\b\d{14}\b])" - This one throws error for the bold part saying illegal escape sequence.

If I get rid of \b - "([\w{11,12}]_POS340BN1-[I|P]-[\d{14}])" - this one picks below file even though there are 15 bytes after character P.

RID12345678__DLF0013B-P-213111181215410.txt

Please help!

CodePudding user response:

remove the [], try this:

([\w{11,12}]_DLF0013B-[I|P]-\d{14})

CodePudding user response:

You not only got rid of the \b: you replaced DLF0013B with POS340BN1 .

You don't seem to have a good understanding of the character set construct: [\w{11,12}] will match one word character or "{" or "," or "}". What you want here is \w{11,12} without the brackets. Also, [I|P] matches "I" or "|" or "P"; I guess you rather want [IP].

  • Related