简体   繁体   中英

Regex to split a string using a character preceded by a particular character

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.

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

零宽度正向后断言。

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

根据sbutler的使用,它使用正向后看。

在C#的Regex.Split方法中使用正向Regex.Split

string[] substrings = Regex.Split("Foo:xxxxxxxxx:Bar:xxxxxxxx:FooBar:xxxxxxx", "(?<=x):");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM