Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Regular expression question.

Posted on 2006-11-03
2
Medium Priority
?
337 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

715 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