regex greedy problem (C#)
Asked Answered
R

5

6

I've a input string like "===text=== and ===text===" and I want to replace wiki syntax with the corresponding html tag.

input:

===text=== and ===text===

desirable output:

<h1>text</h2> and <h1>text</h2>

but with the following code I get this output:

var regex = new Regex("---(.+)---");
var output = regex.Replace("===text=== and ===text===", "<h1>$1</h1>");

<h1>text=== and ===text</h1>

I know the problem is that my regex matches greedy. But how to make them non greedy.

Thank you and kind regards. Danny

Rosewater answered 11/3, 2011 at 16:59 Comment(1)
Try this on for size: regexr.com?2t9heEthic
N
14

Add the question mark to your regex: ===(.+?)===

A better alternative would be to have a regex of the following form: ===([^\=]+)===. See this guide on the dot character for an explanation of using the dot sparingly. When benchmarking my supplied regex, it is approx. 50% faster than your regex.

Nevile answered 11/3, 2011 at 17:5 Comment(0)
M
3

To make a Regex not greedy you use ?

So the expression "===(.+?)===" would have two matches for you - so should allow you to generate <h1>text</h1> and <h1>text</h1>

Metternich answered 11/3, 2011 at 17:2 Comment(0)
T
0

Simply dd a ? maybe?

===.+?===
Tabber answered 11/3, 2011 at 17:3 Comment(0)
H
0

I'll add another variant: ===((?:(?!===).)*)=== (stop catching any character when you encounter ===)... Oh... and for the . problem suggested by WiseGuyEh, I suggest RegexOptions.SingleLine, so that the . match even the newline.

Humanly answered 11/3, 2011 at 17:10 Comment(0)
R
0

And just for info if others have the same issue then I had - to avoid matching also ====Text==== instead of ===Text=== I've extended the pattern like this: (?<!=)===([^=]+)===(?!=)

Rosewater answered 14/3, 2011 at 15:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.