[英]Regex to split a string using a character preceded by a particular character
我有一個字符串,我想使用一個特定字符開頭的字符進行拆分
Foo:xxxxxxxxx:Bar:xxxxxxxx:FooBar:xxxxxxx
我想用冒號分割它:
x
之后。
我嘗試了x:
但是它正在刪除最后一個x
。
我知道我可以使用此正則表達式,然后在每個分割的字符串后附加x
,但是有沒有辦法使用正則表達式分割此字符串,以便最后一個x
也在那里。
嘗試lookbehind assertion
:
(?<=x):
和你的代碼是這樣的:
var result = Regex.Split(inputString, "(?<=x):");
說明:
(?<= subexpression)
Zero-width positive lookbehind assertion.
例如:如果您申請(?<=19)\\d{2}
1851 1999 1950 1905 2003
結果是
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):");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.