开发者

parse part of the text from regex pattern

I have a string:

[\n['-','some text what\rcontains\nnewlines开发者_如何学JAVA'],\n\n

trying to parse:

Regex.Split(@"[\n['-','some text what contains newlines'],\n\n", @"\[\n\['(.*)','(.*)'],.*");

but the split return array seems to be null

i need to get part of text: "some text what contains newlines"


You're looking for the Match function, which will give the the capture groups.

For example:

Regex.Match("[\n['-','some text what\rcontains\nnewlines'],\n\n", @"\[\n\['(.*)','(.*)'],.*", RegexOptions.Singleline).Groups[2].Value

RegexOptions.Singleline is necessary to force . to match \n.


The wildcard '.' does not recognize newlines by default. Use the RegexOptions.Singleline to specify that period should match newlines.

Also checkout Expresso, a great tool for working with C# Regex.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜