开发者

.NET Regex Equivalent of Python Regex's (?P=previousmatchname)

开发者 https://www.devze.com 2023-04-07 22:57 出处:网络
In Python\'s implementation of regular expressions you can do the following: <(?P<foo>.*)>(.*)</(?P=foo)>

In Python's implementation of regular expressions you can do the following:

 <(?P<foo>.*)>(.*)</(?P=foo)>

and basically it will look for <this>right here</this> but not <this>right </here>. Basically it allows you to use the pre开发者_如何学Goviously captured named match later in your regex. Is there a way to do this with .NET's Regex?


You can use sharp brackets or single quotes and use \k to reference them:

<(?<foo>.*)>(.*)</(\k<foo>)>

or

<(?'foo'.*)>(.*)</(\k'foo')>

See this page for more info and scroll down to the "Named Capture with .NET’s System.Text.RegularExpressions" section.


try this:

<(?<foo>.*)>(.*)</\<foo>>
0

精彩评论

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