简体   繁体   English

正则表达式使用前面带有特定字符的字符分割字符串

[英]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 . 我想用冒号分割它: x之后。

I tried x: but it is removing last x . 我尝试了x:但是它正在删除最后一个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. 我知道我可以使用此正则表达式,然后在每个分割的字符串后附加x ,但是有没有办法使用正则表达式分割此字符串,以便最后一个x也在那里。

Try lookbehind assertion : 尝试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 例如:如果您申请(?<=19)\\d{2}

1851 1999 1950 1905 2003 the result is 1851 1999 1950 1905 2003结果是

99 , 50 , 05 995005

零宽度正向后断言。

(?<=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):");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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