Solved

Regular expression question.

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

828 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