Question

I have a string which i want to split using a character preceded by a particular character

Foo:xxxxxxxxx:Bar:xxxxxxxx:FooBar:xxxxxxx

I want to split it using colon : coming after x.

I tried x: but it is removing last x.

I know that i can use this regex and then append x in each splitted string but is there a way to split this string using regex so that last x is also there.

Was it helpful?

Solution

Zero-width positive lookbehind assertion.

(?<=x):

OTHER TIPS

Try lookbehind assertion:

(?<=x):

and your code like this:

var result = Regex.Split(inputString, "(?<=x):");

explain:

(?<= subexpression)
Zero-width positive lookbehind assertion.

for sample: if you apply (?<=19)\d{2} Regex on

1851 1999 1950 1905 2003 the result is

99, 50, 05

var list = Regex.Split("Foo:xxxxxxxxx:Bar:xxxxxxxx:FooBar:xxxxxxx", "(?<=x):");

It uses positive lookbehind, as per sbutler's.

Use positive lookbehind in C#'s Regex.Split method:

string[] substrings = Regex.Split("Foo:xxxxxxxxx:Bar:xxxxxxxx:FooBar:xxxxxxx", "(?<=x):");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top