Get the word in between two words using Regex

Hi all,

i have the string below, its a path to an image and i need to get the name of the image

*C:\Users\SenzoD\Desktop\Automation-Project101\SampleImages\IM-0019-0001.jpeg’

the word i need is IM-0019-0001 and please note this word changes all the time but everything else stays the same

Thanks in advanced

Here is a pattern:

[^\\]+$

for this kind of search I’d tend use Path.GetFileName

1 Like

Hi @msam, thanks for your help, i also need to exclude .jpeg, please help me with that also

(?<filename>[^\\]+)(?<extension>\.\w+)$

You can then use on the resulting match (MyMatch):

MyMatch.Groups("filename").ToString

or Path.GetFileNameWithoutExtension

1 Like

hey buddy @SenzoD
i hope your doing this in for each loop you can do this
use assign value like this strVar = path.GetFileName(item).split("."c)(0)

2 Likes

@SenzoD -
Insted of writting regex logic to get the file name -
Crate a variable ex fileName
fileName = Path.GetFileNameWithoutExtension(“C:\Users\SenzoD\Desktop\Automation-Project101\SampleImages\IM-0019-0001.jpeg”)

2 Likes