开发者

using RegEx to replace string

开发者 https://www.devze.com 2023-01-08 18:19 出处:网络
i have this pattern i using to replace string: var html = \"some test string\"; var regex = new Regex(@\"<(.|\\n)+?>\", RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Multilin

i have this pattern i using to replace string:

var html = "some test string";
var regex = new Regex(@"<(.|\n)+?>", RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Multiline);
var result = regex.Replace(html, ?);

this pattern matches all html tags <anything here> and replace with ?. actually ? is " " or "" according to match type. for example if i using below html markup:

<a href="www.google.com">Google</a><a href="www.yahoo.com">Yahoo!</a>

result is something like below:

Google?Yahoo! (here ? should be " ")

and if i using below html markup:

Buy it now for <b>$279</b>&开发者_运维技巧lt;b>.99</b>!

result is something like below:

Buy it now for ?$279??.99?! (and here ? should be "")

can anybody help to improve this pattern to works properly? thanks in advance

UPDATE

OK, actually i not found an approach to do that i need, so I'm using MatchEvaluator to detect where ? should be "" and where " "! thanks a lot of ;)


Try this for your Regex:

Regex r = new Regex(@"<(.|\n)*?>", RegexOptions.IgnoreCase | RegexOptions.Singleline);

And check your options, there's no need to combine singleLine and MultiLine.


You can use RegEx Coach (http://www.weitz.de/regex-coach/) or http://gskinner.com/RegExr/ (a online tool) to test your regular expressions and get a feeling for them.

0

精彩评论

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