Extract Date From String(Filename)

Hi Community,

Here are four file names which below. I need to extract yyyymmdd from these filenames. How to do this.
CVS_RESPIRATORY_STATSHIP_Comm_20220825020800.csv
HUM_SP_RESPIRATORY_STATSHIP_Comm_20220825_070000.csv
SP_RESPIRATORY_STATSHIP_Comm_20220825010101.csv
ARXWP_RESPIRATORY_STATSHIP_Comm_20220826120000.csv

grafik

1 Like

Hello!

If you need to recover the datetime from a string, you can try using regex:

((19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01]))

This regex will return the date in the pattern yyyymmdd

How to assign the above regular expression to the filename variable ?

Use this in assign:

System.Text.RegularExpressions.Regex.Match(YOUR_STRING_VARIABLE,“((19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01]))”).ToString

It will extract the date to a new variable (String)

Replace YOUR_STRING_VARIABLE, with the respective variable that contains the complete file name (Or hardcoded string)

Screen Shot 2022-08-31 at 14.34.10

System.Text.RegularExpressions.Regex.Match(strText, strPattern).Value
strPattern = "(?<=\_)\d{8}"
strText = YourFileName

[CheatSheet] - System.Text.RegularExpressions | RegEx - News / Tutorials - UiPath Community Forum

Error Message:
Assign: parsing “(?<=_)\d{8}” - Unrecognized escape sequence _.

Thanks. Working Fine

1 Like

vs:

do carefully copy paste - kindly note the backslash

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.