1

I have the following string :

 <span><style>
.G9cT{display:none}
.Q-Bv{display:inline}
</style><span class="G9cT">74</span><span style="display:none">100</span><span class="G9cT">100</span><div style="display:none">100</div><span style="display:none">122</span><span class="G9cT">122</span><div style="display:none">122</div><span style="display:none">178</span><span class="G9cT">178</span><span class="165">189</span><span class="G9cT">202</span><div style="display:none">202</div><span style="display:none">214</span><span class="G9cT">214</span><div style="display:none">214</div><span style="display:none">230</span><div style="display:none">230</div><span class="Q-Bv">.</span><span style="display:none">53</span><span class="G9cT">53</span><span class="G9cT">68</span><div style="display:none">68</div><span style="display:none">81</span><span class="G9cT">81</span><div style="display:none">81</div><span style="display: inline">112</span><span style="display:none">124</span><span class="G9cT">124</span><div style="display:none">127</div><span style="display:none">129</span><span class="G9cT">129</span><span></span><span class="G9cT">151</span><div style="display:none">155</div><span style="display:none">194</span><div style="display:none">194</div><span style="display:none">233</span><span class="G9cT">233</span><span></span><span class="G9cT">234</span><span class="Q-Bv">.</span><span></span><div style="display:none">61</div>88<div style="display:none">98</div><span style="display:none">105</span><span class="G9cT">105</span><div style="display:none">105</div><div style="display:none">117</div><span style="display:none">197</span><span class="G9cT">197</span><div style="display:none">197</div><span style="display: inline">.</span><span class="Q-Bv">65</span><span style="display:none">147</span><div style="display:none">147</div></span>

And i am using Regex to replace the above string with Empty in between two tags <span style="display:none"> * </span> . I am using the code below:

System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex("<span style=\"display:none\">(.*)</span>");
var v = regex.Match(txxt);
string output = System.Text.RegularExpressions.Regex.Replace(txxt, "<span style=\"display:none\">(.*)</span>", "");

But the above replaces the string from first <span style=\"display:none\"> till end.I have tried different combinations. Any advise.

1

1 Answer 1

4

use .*? instead , its lazy , your .* is greedy.

the greedy regex eats up everything after the match it gets. while .*? is reluctant to match and will only match till where the condition is satisfied

Sign up to request clarification or add additional context in comments.

9 Comments

Perfect thank you sir, and what if i only want to replace the tags but not the content inside?
@confusedMind depends on which tag you want to replace. you always have an option of lookahead or lookbehind
@aelor repalce tha tags found using the expression above but not the content inside.
@confusedMind you mean to say that you wan to replace the span style="display:none" with something wlse
@aelor example : replace <span style="display:none">123</span> to only 123 . But i cannot use simple replace function need to use regex as above.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.