Regex to check the pattern is available in String

Hi,

I have list of files in Folder ,I need to check if the File name starts with ‘ABC’ and in the the middle if ‘Draft’ is there then need to consider only that files.
Kindly help with the Regex expression.

Below are the File name:

ABC123 - Draft 13.xlsx
ABC234 - Draft 14.xlsx
ABC567 - Draft 11.xlsx

Hi @yashashwini2322

ABC\d+\s+\-\s+Draft\s+\d+

System.Text.RegularExpressions.Regex.IsMatch(CurrentFile.Name.ToString,"ABC\d+\s+\-\s+Draft\s+\d+")

Regards,

Hi @yashashwini2322

filteredFiles = Directory.GetFiles("C:\Your\Folder\Path").Where(Function(f) Regex.IsMatch(Path.GetFileName(f), "^ABC.*Draft.*\.xlsx$")).ToArray()

Run a For Each loop for filteredFiles

For Each file in filteredFiles
   Log Message -> file
End For Each

Hope it helps!!

@yashashwini2322

^(ABC).*Draft.*

Hello @yashashwini2322

try the following -
^(ABC|abc).Draft..xlsx$

The above expression will check for the file names that start with “ABC” or “abc” which has “Draft” in the middle and has “.xlsx” file extension.

Thanks and Regards,
Ruchir Mahajan.

Hi @yashashwini2322

Instead of using the Regular expressions, use the String Manipulations and use the below condition in If activity,

FileName.toString.Starswith("ABC") andalso FileName.toString.contains("Draft")

Hope it helps!!

@yashashwini2322,

Use this in assign activity to get only the files which are actionable. This will save your bot’s time to iterate unnecessary files.

filteredFiles = Directory.GetFiles("C:\Your\Folder\Path").Where(Function(myfile) Path.GetFileName(myfile).StartsWith("ABC") AndAlso Path.GetFileName(myfile).Contains("Draft"))).ToArray()

Regex will be a strict pattern here, better to go with string methods.

Thanks,
Ashok :slight_smile:

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