开发者

Why do my captures not work in .NET regex?

开发者 https://www.devze.com 2022-12-11 21:59 出处:网络
I\'m parsing some text (admittedly HTML, but it\'s small stuff, and RegEx (should) do the job fine). I\'m trying to use some captures, but they just don\'t do what I think they should.

I'm parsing some text (admittedly HTML, but it's small stuff, and RegEx (should) do the job fine). I'm trying to use some captures, but they just don't do what I think they should.

Match m = new Regex("(.*?)<br>(.*?)/(.*?)/(.*)",
  RegexOptions.None).Match("word<br>stuff1/stuff2/stuff3")
CaptureCollection c = m.Captures;

To my mind, c should contain 4 entries; the stuff in each set of brackets. But it doesn't. Regardless of whether I include any brackets, or all of them, or just the first, my CaptureCollection just contains the original string.

I am missing something about CaptureCollection? Or am I not capturing correctly in the regex?

Thanks for the solution (I'd vot开发者_JAVA百科e up if I could)


You want m.Groups, not m.Captures.

Read about captures on MSDN.


do you have to escape your / with a \, like / ? im not sure, that's something i would try.

Match m = new Regex("(.*?)<br>(.*?)\/(.*?)\/(.*)",
  RegexOptions.None).Match("word<br>stuff1/stuff2/stuff3")
CaptureCollection c = m.Captures;
0

精彩评论

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