Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

url pattern matching 1

Status
Not open for further replies.

Alphabin

Programmer
Dec 8, 2002
119
CA
Hi,

I'm trying to come up with a url pattern matching...
If a URL is not an valid URL don't display it.

What I have so far
Code:
if ($URL =~ /(http:\/\/[0-9A-Za-z._&?=%\/~-]+)/) {
print qq!<a href="$URL">$URL</a> \n!;
}

It's not working so far...
 
This has come up a few times in the past (maybe I'll actually get up off my lazy can make make an FAQ out of something). Anyway, here's a TT thread, and the original DS thread is linked inside: thread219-751655

________________________________________
Andrew - Perl Monkey
 
icrf
Not really what I was looking for...
I simply want a one-line pattern matching exp. to define if the URL is valid.

Thank you for you help

 
Well, if those are all the characters the RFC specifies as valid URI characters, then I guess all you really need to do is anchor it to the beginning and end of the string with ^ and $.
Code:
if ($URL =~ /[COLOR=red]^[/color]http:\/\/[0-9A-Za-z._&?=%\/~-]+[COLOR=red]$[/color]/) {
    print qq!<a href="$URL">$URL</a> \n!;
}
Also, unless you're using the captured $1 group, it's better not to use ( ) at all.

If you're after something to make sure " isn't marked valid, you could try something like I had in that other thread. Just check if it's an [tt]/^($easy|$hard)$/[/tt] and it should be a little better than only checking if it contains valid URI characters.

Hope it helps.

________________________________________
Andrew - Perl Monkey
 
Well it is partially working so far...
My goal is simply to verify if the URL contains the http:// at the beginning. If not, than don't display it.

Should I go with a simpler pattern matching. I came up with this one using OptiPerl.

Let me know..

You get a star for your help. It's really appreciated by the way.

Again, thanks.

 
Well, you don't really to check the entire thing if all you're interrested in is the beginning.
Code:
foreach my $url (qw([URL unfurl="true"]http://www.google.com[/URL]
                    [URL unfurl="true"]http://notreallyalink[/URL]
                    [URL unfurl="true"]www.google.com[/URL]
                    ooga-booga))
{
	print "$url\n" if $url =~ m|^[URL unfurl="true"]http://|;[/URL]
}

________________________________________
Andrew - Perl Monkey
 
Thank you icrf
This was really appreciated.


Again.. Thanks... 2**
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top