Click here to Skip to main content
15,897,718 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hi all, how can i run this regular expression?

XML
string str1 = "<a href=\"http://www.amazon.com/dp/0596528124/\" class=\"someclass\">Mastering Regular Expressions</a>
-- <A href=\"http://cnn.com/\">CNN</a> <div><a href=\"http://blog.ysatech.com\">http://blog.ysatech.com</a></div>";

str1 = System.Text.RegularExpressions.Regex.Replace(str1, "(<[a|A][^>]*>|)", "");
Posted

 
Share this answer
 
Comments
Andy Lanng 17-Aug-15 4:28am    
Wow, .Net tools work in PHP now? :P
(just teasing ^_^)
Hi,

Is some of your code missing? Regex.Replace takes three arguments, your code snippet only has 2, and the regular expression string argument #2
C#
"(<[a|A][^>]*>
isn't terminated. (looks like it needs a closing parenthesis, and a closing double-quote.)
Regex101.com is a great site for testing Regular Expression matching.

If you want the code to run - it looks like C# to me, so create a new Console Application, paste the code in, and run it.

I had success by finishing your "Replace" line as follows:

C#
str1 = System.Text.RegularExpressions.Regex.Replace(str1, "(<[a|A][^>]*>)", "test");


The effect was that all opening anchor tags <a href="......"> were replaced with the string "test". Closing anchor tags were not replaced, neither were the tag contents. Is that what you expected?

Good luck with it....
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900