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.

confusedMind
  • 2,573
  • 7
  • 33
  • 74
  • 3
    yup, check this out: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – G.Y Apr 07 '14 at 05:37

1 Answers1

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

aelor
  • 10,892
  • 3
  • 32
  • 48