简体   繁体   中英

Java Regex pattern not working for filename matching

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!

remove the [] , try this: ([\w{11,12}]_DLF0013B-[I|P]-\d{14})

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].

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM