0

I have a string = "file_20200604170659.DAT,file_20200604170659.TRG,file_20200604170659.CSV"

I need to write a Regular Expression to get the file that contains the given output type.

For Example: If I ask for CVS, I just want to get file_20200604170659.CSV from the above string.

Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397
  • If your language's regex supports *lookbehinds* and you wish to match `'CSV'` files, you might write `(?<=^|,)file_\d{14}\.CSV\b`. `(?<=^|,)` is a *positive lookbehind* (`?<=`) that asserts the match start at the beginning of a line (`^`) or (`|`) be immediately preceded by a comma. `\b` is a word boundary, which prevent `'CSV'` to be followed by a word character (e.g., `'CSVV'`). The match would be `'file_'` followed by 14 digits, followed by `'.CSV'`. This would match `'file_20200604170659.CSV'`. [Demo](https://regex101.com/r/XwpbLX/2/) – Cary Swoveland Jun 04 '20 at 21:34

0 Answers0