I am looking for a regex that looks for any $$some_val$$
and replaces the some_val
with uppercase letters.
For example the input is:-
<p><a href='accept/272/$$id$$'>YES</a></p>
<p>Hi $$FirstName$$ some more text $$date$$ lorem ipsum</p>
<h1>$$club$$</h1>
$$content$$
would output:-
<p><a href='accept/272/$$ID$$'>YES</a></p>
<p>Hi $$FIRSTNAME$$ some more text $$DATE$$ lorem ipsum</p>
<h1>$$CLUB$$</h1>
$$CONTENT$$
at the moment I have the following regex:-
var html = Regex.Replace(html, @"\$\$(.*)\$\$", m=> m.Value.ToUpper());
but it produces the incorrect result.
<p><a href='accept/272/$$ID$$'>YES</a></p>
<p>Hi $$FIRSTNAME$$ SOME MORE TEXT $$DATE$$ lorem ipsum</p>
<h1>$$CLUB$$</h1>
$$CONTENT$$
as the SOME MORE TEXT
is also capitalised between the start and end $$ delimiters.
Please note that $$ might reoccur in a line OR start/end on a line.
You just need to use the non-greedy/lazy match quantifier ( *?
):
var regex = new Regex(@"\$\$.*?\$\$");
var input = "this $$is a$$ test of the $$procedure$$";
var output =
r.Replace(input, m=>m.Value.ToUpper());
try this , it should works :
var html = Regex.Replace(html, @"\$\$(.\w*)\$\$", m=> m.Value.ToUpper());
i have test it on : http://rubular.com/r/YRI3WrzXAu
You need to use the lazy *?
instead of the eager *
. *
by itself tries to match as much as possible, *?
tries to match as little as possible.
var html = Regex.Replace(html, @"\$\$(.*?)\$\$", m=> m.Value.ToUpper());
Another option could be to use a lookahead assertion. For example:
var html = Regex.Replace(html, @"\$\$(?:[^$]|\$(?!\$))*\$\$", m => m.Value.ToUpper());
This would search for the two dollars, then search for anything that is not a dollar OR a dollar that is not succeeded by another dollar. Greediness in this case would not matter due to the lookahead assertion.
This is a bit more advanced than the dot-star you've been using, so if dot-star works for you, then it may be more realistic to stick with that.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.