Solved

Regular expression question.

Posted on 2006-11-03
2
328 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

803 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