Matching URls

Discussion in 'Perl Misc' started by Seansan, Jul 20, 2003.

  1. Seansan

    Seansan Guest

    Hi,

    Does anyone know of a link to or an example of a decent regexp that wil
    recognize internet URLs? (It needs to match urls starting with http:// and
    www.)

    I am trying to replace a string like
    "My Homepage is @ http://www.homepage.nl"
    with
    "My Homepage is @ <A HREF =
    'http://www.homepage.nl'>http://www.homepage.nl</A>"

    Thx, Seansan
     
    Seansan, Jul 20, 2003
    #1
    1. Advertising

  2. On 20 Jul 2003, Tina Mueller wrote:

    >Seansan wrote:
    >
    >> Does anyone know of a link to or an example of a decent regexp that wil
    >> recognize internet URLs? (It needs to match urls starting with http:// and
    >> www.)

    >
    >perldoc URI::Find
    >and
    >perldoc URI::Find::Schemeless


    Damn those URIs and their zany schemes.

    --
    Jeff Pinyan RPI Acacia Brother #734 2003 Rush Chairman
    "And I vos head of Gestapo for ten | Michael Palin (as Heinrich Bimmler)
    years. Ah! Five years! Nein! No! | in: The North Minehead Bye-Election
    Oh. Was NOT head of Gestapo AT ALL!" | (Monty Python's Flying Circus)
     
    Jeff 'japhy' Pinyan, Jul 20, 2003
    #2
    1. Advertising

  3. Seansan

    JR Guest

    "Seansan" <sheukels=cuthere=@yahoo.co.uk> wrote in message news:<3f1aa893$0$61643$4all.nl>...
    > Hi,
    >
    > Does anyone know of a link to or an example of a decent regexp that wil
    > recognize internet URLs? (It needs to match urls starting with http:// and
    > www.)
    >
    > I am trying to replace a string like
    > "My Homepage is @ http://www.homepage.nl"
    > with
    > "My Homepage is @ <A HREF =
    > 'http://www.homepage.nl'>http://www.homepage.nl</A>"
    >
    > Thx, Seansan


    ## I think this is what you want. Good luck.
    ## JR

    #!/perl
    use strict;
    use diagnostics;
    use warnings;

    while(<DATA>) {
    s/(http:\/\/www.*)\b/<a href='$1'>$1<\/a>/g;
    print $_, "\n";
    }

    __DATA__
    My homepage is @ http://www.homepage.nl
    My favorite site is @ http://www.espn.com
    My second most favorite site is @ http://www.whatever.org

    =pod
    ## OUTPUT

    My homepage is @ <a href='http://www.homepage.nl'>http://www.homepage.nl</a>
    My favorite site is @ <a
    href='http://www.espn.com'>http://www.espn.com</a>
    My second most favorite site is @ <a
    href='http://www.whatever.org'>http://www.whatever.org</a>

    =cut
     
    JR, Jul 20, 2003
    #3
  4. Seansan

    Bob Walton Guest

    Seansan wrote:

    ....


    > Does anyone know of a link to or an example of a decent regexp that wil
    > recognize internet URLs? (It needs to match urls starting with http:// and
    > www.)

    ....


    > Thx, Seansan


    You could:

    use Regexp::Common::URI;

    --
    Bob Walton
     
    Bob Walton, Jul 20, 2003
    #4
  5. Seansan

    Bob Walton Guest

    Bob Walton wrote:

    ....
    > You could:
    >
    > use Regexp::Common::URI;
    >

    Make that:


    use Regexp::Common qw(URI);

    --
    Bob Walton
     
    Bob Walton, Jul 20, 2003
    #5
  6. Seansan

    Seansan Guest

    Thx JR (and others),

    I tried it, and it works. But I only have 1 problem:

    *) It matches all the text after the url as well."My String: "Homepage is @
    http://www.homepage.nl, yep thats it" becomes
    "Homepage is @ <A HREF=http://www.homepage.nl yep thats
    it>http://www.homepage.nl yep thats it</A>"

    Any ideas on how to solve this? I played witht he regexp a while, but I cant
    figure it out.

    ps. How would I alter the regexp to match www. also? Like this?
    s/([http:\/\/www|www].*)\b/<a href='$1'>$1<\/a>/g;

    Seansan


    "JR" <> wrote in message
    news:...
    > "Seansan" <sheukels=cuthere=@yahoo.co.uk> wrote in message

    news:<3f1aa893$0$61643$4all.nl>...
    > > Hi,
    > >
    > > Does anyone know of a link to or an example of a decent regexp that wil
    > > recognize internet URLs? (It needs to match urls starting with http://

    and
    > > www.)
    > >
    > > I am trying to replace a string like
    > > "My Homepage is @ http://www.homepage.nl"
    > > with
    > > "My Homepage is @ <A HREF =
    > > 'http://www.homepage.nl'>http://www.homepage.nl</A>"
    > >
    > > Thx, Seansan

    >
    > ## I think this is what you want. Good luck.
    > ## JR
    >
    > #!/perl
    > use strict;
    > use diagnostics;
    > use warnings;
    >
    > while(<DATA>) {
    > s/(http:\/\/www.*)\b/<a href='$1'>$1<\/a>/g;
    > print $_, "\n";
    > }
    >
    > __DATA__
    > My homepage is @ http://www.homepage.nl
    > My favorite site is @ http://www.espn.com
    > My second most favorite site is @ http://www.whatever.org
    >
    > =pod
    > ## OUTPUT
    >
    > My homepage is @ <a

    href='http://www.homepage.nl'>http://www.homepage.nl</a>
    > My favorite site is @ <a
    > href='http://www.espn.com'>http://www.espn.com</a>
    > My second most favorite site is @ <a
    > href='http://www.whatever.org'>http://www.whatever.org</a>
    >
    > =cut
     
    Seansan, Jul 20, 2003
    #6
  7. "Seansan" <sheukels=cuthere=@yahoo.co.uk> wrote in message news:<3f1aa893$0$61643$4all.nl>...
    > Hi,
    >
    > Does anyone know of a link to or an example of a decent regexp that wil
    > recognize internet URLs? (It needs to match urls starting with http:// and
    > www.)


    Check out the Regexp::Common module. I think it has what you are looking for.

    >
    > I am trying to replace a string like
    > "My Homepage is @ http://www.homepage.nl"
    > with
    > "My Homepage is @ <A HREF =
    > 'http://www.homepage.nl'>http://www.homepage.nl</A>"
    >
    > Thx, Seansan
     
    Patrick LeBoutillier, Jul 20, 2003
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kaidi
    Replies:
    5
    Views:
    493
    Andrew Thompson
    Jan 4, 2004
  2. Nathan Sokalski

    Converting Relative URLs into Absolute URLs

    Nathan Sokalski, Aug 11, 2008, in forum: ASP .Net
    Replies:
    1
    Views:
    764
    Sriram Srivatsan
    Aug 12, 2008
  3. Adam Monsen

    JDBC URLs ...not really URLs?

    Adam Monsen, Feb 6, 2009, in forum: Java
    Replies:
    11
    Views:
    6,335
    Adam Monsen
    Feb 8, 2009
  4. Steve T.

    dynamic URLS convert to static URLS for search engines

    Steve T., Mar 1, 2004, in forum: ASP .Net Web Services
    Replies:
    7
    Views:
    311
    Steve T.
    Mar 4, 2004
  5. Marc Bissonnette

    Pattern matching : not matching problem

    Marc Bissonnette, Jan 8, 2004, in forum: Perl Misc
    Replies:
    9
    Views:
    251
    Marc Bissonnette
    Jan 13, 2004
Loading...

Share This Page