Help me making Regex

Hi i need ur help to produce a regex .

string example :
NA_ARG_510012332_0231212_51231231N15022_55234782110.pdf

output:
i want to only take the last part of the string which is 55234782110 = this value will also random each time ,
so the indicator is to take every characters after the lase “'-”/underscore.
and everything before the last ‘-’/underscore will be ignored
take note everything before the last ‘_’ /underscorewill be random characters

Thank you

@Ahmad_Rais

You need not have regex instead can you try this

say your string is in str then

str.Substring(str.LastIndexOf("_")+1).Replace(".pdf","")

If you need regex only then (?<=_)\d+(?=.pdf)

But best would be using substring and Replace

cheers

1 Like

Hi @Ahmad_Rais

You can try with with split expression

Split("NA_ARG_510012332_0231212_51231231N15022_55234782110.pdf","_").Last
System.Text.RegularExpressions.Regex.Replace(Split("NA_ARG_510012332_0231212_51231231N15022_55234782110.pdf","_").Last,".pdf","").ToString

image

Regards
Gokul

ooo we can use .lastindexof(“somecharacters”)+! , i never knew this
btw whats the need of the “+1” ??.

then if i need to take everything including the .pdf i will use just
str.Substring(str.LastIndexOf(“_”)+1) ??

thanks

Hi @Ahmad_Rais

Another Method with Regex.Split

System.Text.RegularExpressions.Regex.Split(Split("NA_ARG_510012332_0231212_51231231N15022_55234782110.pdf","_").Last,".pdf")(0)

image

Regards
Gokul

1 Like

Hi,

How about the following expression?

 System.Text.RegularExpressions.Regex.Match(yourString,"\d+",System.Text.RegularExpressions.RegexOptions.RightToLeft).Value

Regards,

1 Like

@Ahmad_Rais

Yes you can use it with out replace as well to get with .pdf

str.Substring(str.LastIndexOf("_")+1)

+1 is because you need data after '_' right… LastIndexOf("_") gives the index of '_' .So to get the data from next value we need to add 1

If this is good. Please mark the solution and close so that others also can get help

cheers

cheers , i will try it shortly on some scenario and ofc i will mark it as solution !

thanks

1 Like

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