Creating Substring according to repeated string

Hi! I have a string like below:

SS23457
DG
1.2
0.7
SS456
4.5
FR
SS786
345
DD

I want to split this string according to “SS” string. For instance, first substring will be:
SS23457
DG
1.2
0.7
Second will be:
SS456
4.5
FR
and so on.
How can I do that?

@AnnaDewitt - did you try Regular Expression - easy to do this with RegEx.

Yes, I tried this RegEx:

(?=SS)([\S\s]*)(?=SS)

But with this way, it starts from first SS and ends with last SS. It does’nt take the first part, takes whole string.

Is there a way to detect just next SS?

Hi. You can use
arr = text.Split({"SS"},StringSplitOptions.RemoveEmptyEntries)
and then append “SS” to the beginning
arr = (From s in arr Select "SS"+s).ToArray

1 Like

What is “s” in this case? Is it array index?

In a second line? A local variable. An array element, not index. We initialize it after keyword From

Another possible solution:

SS\d+((.|\r?\n)(?!SS\d))*

1 Like

@AnnaDewitt - try ending with -> (?=\nSS)

Thank you! It seems so simple but I tried to do that almost 4 hours :sleepy:

Thank you for your help. Solution of Yurii_Horobets works but I tried your solution too, because I want to learn these regex expressions. I realised a mistake the text I’ve shared. After “SS” part there is a letter before numbers. Because of this, your regex expression didn’t work. How can I changed that expression to work with this:

SSC23457567
5
FR
SDE
2.0/TE
1.2/TE
CTRKE
SSK45652346
KB
FGH
1.2/TE
0.2/TE
PLFK
SSP78696854
DS
MKN
2.5/TE
1.7/TE
TKLE

Sorry for sending wrong text. I tried to use your expression to works with this text but I didn’t quite understand these regex expressions yet.

Just change \d to \w since you also want to match both digits and letters after SS:

SS\w+((.|\r?\n)(?!SS\w))*

1 Like

Thank you! Lastly, can I use these matches seperately like element of an array?

The result of Matches() is collection of Match. You can use matchesResult(0).Value to get the first match, matchesResult(1).Value to get the second match, etc. Or you could use a For Each loop to go through the matches.

If you want to convert the matches to an array of string:

myArray = matchesResult.OfType(Of Match)().Select(Function(m) m.Value).ToArray

Make sure that you have imported the System.Text.RegularExpressions namespace.

1 Like

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