Solved

Regular expression question.

Posted on 2006-11-03
2
324 Views
Last Modified: 2010-04-16
Hello all,

I have the following scenario:

Retrieve data from third party via a WebClient object, and load retrieved content into a StringBuilder object.

From there, I want to replace all matches with my own version:

Given the following string "<a href="http://www.xxx.com/cgi?jvsdhjarewh"v ghjgh="">link</a><a href="http://www.xxx.com/cgi?jvsdhjarewh">link</a><a href="http://www.xxx.com/cgi?hjhkhkj">link</a>"

I want to look for the following pattern "<a href="(http://www.xxx.com/\S*?)""

And replace all matches with "<a href="http://www.x.asp?page=$1""

In practice, this works, and returns "<a href="http://www.x.asp?page=http://www.xxx.com/cgi?jvsdhjarewh"v ghjgh="">link</a><a href="http://www.x.asp?page=http://www.xxx.com/cgi?jvsdhjarewh">link</a><a href="http://www.x.asp?page=http://www.xxx.com/cgi?hjhkhkj">link</a>"

However, I must also have the matched pattern ($1) replaced as a URLEncoded string, otherwise the above result will work properly.

I don't know the MatchEvaluator, but wondered if this might be the place to add my own function for this purpose? Or is there a nice little Regexp function that I can enclose my $1 match with?

Thanks for your help.
0
Comment
Question by:AlfaNoMore
2 Comments
 
LVL 13

Accepted Solution

by:
joechina earned 500 total points
ID: 17869237
static void Main(string[] args)
        {
            string s = "<a href=\"http://www.xxx.com/cgi?jvsdhjarewh\"v ghjgh=\"\">link</a><a href=\"http://www.xxx.com/cgi?jvsdhjarewh\">link</a><a href=\"http://www.xxx.com/cgi?hjhkhkj\">link</a>";
            MatchEvaluator me = new MatchEvaluator(ReplaceFunc);
            string result = Regex.Replace(s, "(?<=href=\")(?<link>http://www.xxx.com/[^\"]*)", me);
            Console.WriteLine(result);
            Console.Read();
        }
        static string ReplaceFunc(Match m)
        {
            return "http://www.x.asp?page=" + HttpUtility.UrlEncode(m.Groups["link"].Value);
        }
0
 
LVL 9

Author Comment

by:AlfaNoMore
ID: 17880076
Thanks joechina, simple as that eh? :-)
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now