Split a string based on Regex

@ClaytonM @arivu96 can we split string based on variable(variable value changes everytime.

Hi @Manjuts90,
Yes we can split.

string data = "THExxQUICKxxBROWNxxFOX"

Arrvalue=data.Split(new string[] { "xx" }, StringSplitOptions.None)

Regards,
Arivu

@arivu96
i have more doubt, if string contains
Ex:-
kira he 234.23
kirad he1w huwe 423.342
kirasdasd df2sh fhsjf dhsj 7838.09
Kiran hi h2i hi gdhs 95321.098

In example it may many more lines from each line i want number in last in one variable and rest of it another variable for each line

Hi @Manjuts90,
Use this pattern

Pattern:
([\d]+[.][\d]+|[\d]+)$

Regards,
Arivu

1 Like

@arivu96 I got answer by using ur pattern what is the meaning of it

Hi @Manjuts90,

Refer this

Regards,
Arivu

@arivu96 thanks

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.

1 Like

@ClaytonM Thanks

@arivu96 @lainh @ClaytonM how find the first and last digit position in following strings using Regex. Can anyone help me to solving this.

Example: “dsgjdg1234dskjk”
“dseyireui1234ndcsm”
“267352dsdhhskj”

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.