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
Anil_G
(Anil Gorthi)
December 20, 2022, 7:43am
2
@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
Gokul001
(Gokul Balaji)
December 20, 2022, 7:44am
3
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
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
Gokul001
(Gokul Balaji)
December 20, 2022, 7:45am
5
Hi @Ahmad_Rais
Another Method with Regex.Split
System.Text.RegularExpressions.Regex.Split(Split("NA_ARG_510012332_0231212_51231231N15022_55234782110.pdf","_").Last,".pdf")(0)
Regards
Gokul
1 Like
Yoichi
(Yoichi)
December 20, 2022, 7:46am
6
Hi,
How about the following expression?
System.Text.RegularExpressions.Regex.Match(yourString,"\d+",System.Text.RegularExpressions.RegexOptions.RightToLeft).Value
Regards,
1 Like
Anil_G
(Anil Gorthi)
December 20, 2022, 7:47am
7
@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
system
(system)
Closed
December 23, 2022, 7:52am
9
This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.