Issue
Below are the files in a folder and wanted to write a regex pattern to match all the filenames and separate it like into 4 groups like
Groups:
- Text before date pattern
- Date Pattern
- Text after date pattern
- Extension (any or no extension)
Names:
XYZ_XY__T_20180808_88
GYG_20180813.csv
JENNY_BH_COSTUMES_T_20180808_88.csv
JKS9KS9_DDD_20180809_2.txt
AMY_BH_MAKEUP_T_20180808_88.dat
UUB-134941099-00002531-003_20180814
usa-Nasa_Y_20180806_01.csv
usa-Tpkyo-HHDY_Y_20180806_01.csv
Tried this -
(\w+)(-?)_(\d{4}\d{2}\d{2})(\w+)?(\.csv|\.dat|\.txt)?
but doesn't seem to work. How to go about this?
Solution
Since you want to capture all the text before the date substring regardless, it looks like all you have to do is include dashes and underscores in the initial group: ([\w-_]+)
(and drop the following group that captures an optional standalone dash):
You may also use ^
and $
to ensure that matches span the entire line:
^([\w-_]+)(\d{4}\d{2}\d{2})(\w+)?(\.csv|\.dat|\.txt)?$
https://regex101.com/r/M5kIo6/1
Answered By - CertainPerformance
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.