Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Regular expression question.

Posted on 2006-11-03
2
Medium Priority
?
339 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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month12 days, 12 hours left to enroll

971 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