Extract substring from directory path between symbols

Hi,

I am attempting to try and extract two separate strings from a file name in a directory path - I am able to extract the file name from the path but I want to be able to extract two separate substrings that is beween the last “/” and first underscore symbol _, and the second substring is between the first underscore symbol _ and second _

An example path would be: “C:\Users\Robot\Documents\UiPath\ExtractedFolder\123456_78912345_Test_File_2020.pdf”

So the first substring would be - 123456
Second substring - 78912345

I managed to get the entire file name alone but need to extract two substrings from the file name.
Closest I was able to get was: file.ToString.Substring(file.ToString.LastIndexOf("")+"_".Length).Split(Environment.NewLine.ToCharArray)(0)

Any suggestions would be great please.

Hi

I have two options/ potential solutions:

I have used a combination of string Split and Regex Replace activity. (Still working on my string manipulation skills).

image

I have found this String Manipulation post to be very helpful :slight_smile:

OPTION 2: Use Regex Groups
.*(?<=\)([^]+)([^_]+)
Check out this Regex Pattern preview

image

If you want to learn Regex - check out my Regex Megapost

2 Likes

@ciaramkm Here’s another possible solution you can try:

Filename = Path.GetFileName("C:\Users\Robot\Documents\UiPath\ExtractedFolder\123456_78912345_Test_File_2020.pdf")
PartsArray = Filename.Split("_"c)
Part1 = PartsArray(0)
Part2 = PartsArray(1)

Filename, Part1 and Part2 are strings.
PartsArray is an array of strings.

1 Like