.NET Framework Bookmark and Share   
 index > Regular Expressions > How can I find and replace url by regular expression?
 

How can I find and replace url by regular expression?

Hi all,

for example,

http://www.myurl.edu/.../.../pages/test.aspxCase 1 (Include myurl.edu or start from http://www.myurl.edu)
http://www.myurl.edu/.../.../.../pages/test2.aspxCase 2
/.../.../.../.../pages/test2.aspxCase 3 (Start from "/")


http://www.notmyurl.net/.../.../pages/test.aspxCase 5

I want to use regular expression tofind case 1 ~3 to remove /pages/ and replace aspx to htm... e.g. 1
from: http://www.myurl.edu/1/2/pages/test.aspx
to: http://www.myurl.edu/1/2/test.htm

or

e.g. 2
from: /mylocalpath/1/2/pages/test.aspx
to: /mylocalpath/1/2/test.htm


Could you please advice me how can I do? Many thanks for you^^
.NET Beginner 3.5
Hello World August
The basic URL search pattern is this:

(?<Protocol>\w+):\/\/(?<Domain>[\w@][\w.:@]+)\/?[\w\.?=%&=\-@/$,]*

This will give you the protocol and domain in its own capture groups like so:

//Example
http://www.myurl.edu/.../.../pages/test.aspx

Protocol --> http
Domain --> www.myurl.edu

In your case you probably might need a MatchEvaluator to do some checking. Perhaps start with this expression:

(?<Prefix>.*)(?<Suffix>pages\/test\d?\.aspx)

Then use the MatchEvaluator to check the prefix to see if it is clear to replace. If it is, make the replacement in the Suffix.
John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
JohnGrove
Take a look a this site, it has alot of different regexp examples.
http://regexlib.com/DisplayPatterns.aspx?cattabindex=1&categoryId=2

Eugene Mikhaylov http://www.codelibrary.ru
Eugene Mikhaylov

Oh, sorry all, I passed this case to my classmate and he said its work in previous month�/p>

When I test the application, I think we don’t know how to handle “Regex�and “MatchEvaluator�class for .NET application�br/>
Therefore I unmarked answer (I will remark it later), and just voted as helpful...

Anyway, thank you Eugene.

In short, could you please advise me how to use .NET code to replace the following Url?

e.g.1.

From: http://www.myurl.edu/1/2/pages/test.aspx

When start with http://www.myurl.edu/:
To http://www.myurl.edu/1/2/test.htm

=============================================================

Besides, can I replace the url ifit is placed in html file?

e.g.2

From: <a href="http://www.myurl.edu/1/2/pages/test.aspx">http://www.myurl.edu/1/2/pages/test.aspx</a>

To: <a href=" http://www.myurl.edu/1/2/test.htm"> http://www.myurl.edu/1/2/test.htm</a>

I will remark answer later, thank you very much for your attention.


.NET Beginner 3.5
Hello World August

And our testing code as belows: (From http://msdn.microsoft.com/en-us/library/cft8645c.aspx)

using System;
using System.Text.RegularExpressions;

class RegExSample
{
    static string CapText(Match m) // We don't know how to use this ...>o<
    {
        // Get the matched string.
        string x = m.ToString();
        // If the first char is lower case...
        //if (char.IsLower(x[0]))
        //{
        //    // Capitalize it.
        //    return char.ToUpper(x[0]) + x.Substring(1, x.Length - 1);
        //}
        //return x;

        x = x.Replace("/pages/", "/");
        x = x.Replace(".aspx", ".htm");

        return x;


    }

    static void Main()
    {
        string text = "http://www.myurl.edu/test1/test2/pages/test3.aspx";

        System.Console.WriteLine("text=[" + text + "]");

        Regex rx = new Regex(" ....  "); // Error occured when pass expression to this function 

        string result = rx.Replace(text, new MatchEvaluator(RegExSample.CapText)); //Cannot replace by this code ...

        System.Console.WriteLine("result=[" + result + "]");
    }
}



Many thanks for any advice &... Sorry for any inconvenience caused.


.NET Beginner 3.5
Hello World August
Rather then unmarking the post because you have additional questions, you should post again.
John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
JohnGrove

You can use google to search for other answers

Custom Search

More Threads

• Filtering in a Datatable
• Get Number inside <div>
• Splitting a sentence into words and nonwords
• Problem when using Regex.Split
• Enhanced Regex
• Strange behavior: match.Value produced by exp1 | exp2 doesn't equal value produced by exp2 | exp1. Can anyone explain?
• LookAround help
• Regular expression for date 'yyyy-mon-dd' format
• Find a string between 2 words including them
• Application seems no response if I use this Regex