开发者

Regular expression problem, skipping unkown amount of matches?

开发者 https://www.devze.com 2023-02-05 12:02 出处:网络
I am using c# but its basically not really important, my question is: is it possible, only with regular expression skipping matches?

I am using c# but its basically not really important, my question is: is it possible, only with regular expression skipping matches?

my regex was

Text = Regex.Replace(Text, @"\[code1\]((.|\n)*?)(\<\/span\>) ", "<span class='spanTest'>$1</span>");

but the problem with this, is greedy... allways gets the first match of the closing span

I have a string similar like this:

[code1]test string bladibla[code2]code2 string</span> [code3] code3 string [code1]&a开发者_运维知识库mp;nbsp; </span></span> end of the span string </span>

and my ending result should be something like:

<span class="spanTest">test string bladibla[code2]code2 string</span> [code3] code3 string [code1]&nbsp; </span></span> end of the span string </span>

Do you guys have any suggestions?


Assuming that substrings starting with "[code]" and ending with "</span>" can be nested inside each other, and you are trying to find matching pairs of them, the answer is no. Regular expressions can't match nested patterns, it's one of their theoretical limitations. (I know that's not a solution, but it is permission to stop banging your head on this particular wall.)


I can't follow you. Your regex is definitely not greedy (*? is a lazy quantifier), so it will match from [code1] to the closest </span> it finds, just as in your example.

However, it will also match from the second [code1] in your sample until the following </span>. Is it this behaviour you're trying to prevent? If so, what is the difference between the two cases? Do you only want to match the first occurrence, or do you only want to match it at the start of the string, or...?

Finally, instead of ((.|\n)*?) you can write (.*?) if you specify RegexOptions.Singleline.

0

精彩评论

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