开发者

Omitting end of the result found from a regular expression

开发者 https://www.devze.com 2022-12-20 05:27 出处:网络
I have this regular expression in c#: Regex oRegex_ = new Regex(\"<!-- #BeginEditable \\\"Body\\\"[^>]*>(.*)<!-- #EndEditable [^>]*>\", RegexOptions.Mu开发者_高级运维ltiline);

I have this regular expression in c#:

Regex oRegex_ = new Regex("<!-- #BeginEditable \"Body\"[^>]*>(.*)<!-- #EndEditable [^>]*>", RegexOptions.Mu开发者_高级运维ltiline);
MatchCollection matches_ = oRegex_.Matches(contents);

The variable called 'contents' equals this:

<!-- #BeginEditable "Body" -->First String<!-- #EndEditable --><!-- #BeginEditable "Extra" -->Second String<!-- #EndEditable -->

Currently it doesnt seem to grab what i need, which is

'First String'

Instead it grabs

'First String<!-- #EndEditable --><!-- #BeginEditable "Extra" -->Second String<!-- #EndEditable -->'

Can anyone help me solve this using a regular expression? :(

Please Note: i will be replacing 'Body' to become a variable and then put it all in a loop so i can extract 'Second String' by making the variable equal 'Extra'.


Do a non greedy catch using *?:

<!-- #BeginEditable \"Body\"[^>]*>(.*?)<!-- #EndEditable [^>]*>

Another option is to catch the right character (not <, but this is less accurate if you allow <s in your body):

<!-- #BeginEditable \"Body\"[^>]*>([^<]*)<!-- #EndEditable [^>]*>
0

精彩评论

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