开发者

parse part of the text from regex pattern

开发者 https://www.devze.com 2022-12-24 17:33 出处:网络
I have a string: [\\n[\'-\',\'some text what\\rcontains\\nnewlines开发者_如何学JAVA\'],\\n\\n trying to parse:

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

精彩评论

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

关注公众号