HI @Manjuts90
There has a reference about Regex pattern:
Wish this can help.
@lainh Thanks
@arivu96 why you used “|[\d]+” in Pattern("([\d]+[.][\d]+|[\d]+)$) without “|[\d]+” also working fine. can i know reason using it.
Hi @Manjuts90
Excuse me for cutting in.
Without “|[\d]+” ,it could not work accurately under certain circumstances like :
“Kiran hi h2i hi gdhs 95321”
Because that there has none “.” , it can not be split into 2 string.
@lainh Thanks
Additionally to the Regex solutions, if your text is always the last word in a string, you can could probably just use the normal Split (where “c” is for character) and .Last
lastvalue = txt.Split(" "c).Last
then, you can use .Take to extract all the other words and .Join to combine it to one string.
allbutlastvalue = String.Join(" ", txt.Split(" "c).Take(txt.Split(" "c).Count-1) )
Lastly, since you have multiple lines, you can either 1) run it through a loop and extract the values as you go, or 2) create a new list with it already extracted.
To extract the values to a new list you can use .Select, in my opinion.
lastvalues = fulltext.Split({System.Environment.Newline},System.StringSplitOptions.RemoveEmptyEntries).Select(Function(x) If( x.ToString.Contains(" "), x.ToString.Split(" "c).Last, "0" ) ).ToArray
and
othervalues = fulltext.Split({System.Environment.Newline},System.StringSplitOptions.RemoveEmptyEntries).Select(Function(x) If( x.ToString.Contains(" "), String.Join(" ", txt.Split(" "c).Take(txt.Split(" "c).Count-1) ), "" ) ).ToArray
The above examples will basically create an array of the last words and an array of the all other words.
To join it back together to one string, you can just use .Join again.
String.Join(System.Environment.Newline, lastvalues)
I hope this is also helpful.
Regards.
@ClaytonM Thanks
When you say “position” of the first and last digit are you looking for the index of that digit?
E.g. Based on your example string “dsgjdg1234dskjk”, are you trying to get results of index=6 for first digit and index=9 for your last digit?
Or are you trying to actually pull out the digits?
E.g. Based on your example string “dsgjdg1234dskjk”, are you trying to find the first digit, find the last digit, and get everything between the two, including the digits themselves? So your intended result would be “1234”? Additionally, if this is your intention, if you had a string of “dsgjdg12a34dskjk” would you want to have the result be “12” and “34” or would you want a result of “12a34”?
@Dave “Based on your example string “dsgjdg1234dskjk”, are you trying to get results of index=6 for first digit and index=9 for your last digit?” i want this result
To get index of first digit: YourStringVariable.IndexOfAny("0123456789".ToCharArray())
To get index of last digit: YourStringVariable.LastIndexOfAny("0123456789".ToCharArray())
@Dave i just want regex pattern to find those positions not other methods i required it for something bro
Why do you require regex in this instance? It is slower than the method I just posted.
Generally you would use regex to pull out the exact string, not to find an index.
@Dave i want to get that numbers only
example: asd12da34ds result= 1234
ashd123 result=123
asd34dsj result=34
1234dsjkd result=1234
Ok i’m a bit confused because earlier you had stated (bold=mine):
Manjuts9025m
@Dave “Based on your example string “dsgjdg1234dskjk”, are you trying to get results of index=6 for first digit and index=9 for your last digit?” i want this result
Assuming whitespaces should separate out different numbers/results, then you are looking for this regex pattern: \d[\w]*\d
How it works:
\d = finds any digit.
[\w]* = finds any non white-space characters. The asterisk * matches the preceding character 0 or more times (so it finds as many non white-space characters until the next piece)
\d = finds any digit. As this is the last piece in the regex statement, it will find the last digit and the previous [\w]* will stop searching for more non white-space characters.
If you want whitespace characters returned in your match, then you should instead use \d.*\d
This works the same way as the one above, but replaces [\w]* with .* The period . matches (almost) any character. It is quite slow though, so often it is better to use a character class or negated characters instead if at all possible.
EDIT: Based on your questions above it seems like you’re using regex in your workflows a decent amount. I’d highly recommend reading up a bit on regex at https://www.regular-expressions.info/ as it is a fantastic resource. I also use .NET Regex Tester - Regex Storm to test out my .NET regex expressions and their quick reference at .NET Regex Reference - Regex Storm can come in handy as well.
Hi @Manjuts90,
Refer this post
Copy to Another excel but only the numbers - #4 by arivu96
System.Text.RegularExpressions.Regex.Replace(value1.ToString(), "[^0-9]+", string.Empty)
Regards,
Arivu
Hi @Manjuts90
I think the formula below will works :
System.Text.RegularExpressions.Regex.Split(varTarget,"[^0-9]+",System.Text.RegularExpressions.RegexOptions.None).Where(Function(x) x<>"").ToArray()
Regards.
@arivu96 "ab"c regex returns first occurrence position of substring “ab” in String “abdshjabdjskabdjs”. i want last occurrence position of substring “ab” in above. i want regex pattern for this. Can you help me on this.
@lainh "ab"c regex returns first occurrence position of substring “ab” in String “abdshjabdjskabdjs”. i want last occurrence position of substring “ab” in above. i want regex pattern for this. can you help me on this.
Hi @Manjuts90,
From this post i dont know what do you need. Could you give some example ? And clearly mark which phrase you need to cut out ?
If you still need isolate number here you got regex: (?<Text>(?:\s)?(?:\D*))(?<Number>(?:\s)?(?:\d*)\s?)