Why don't you just get rid of the stuff in front of the www.foo.com stuff? I.e., assume its "bad" and put "http://" in front of it? Or for that matter just leave the http:// off once you've done step (1).
#!/usr/bin/perl -w
use strict;
my @urls = ('tp://www.foo.com/' , '://www.foo.com',
'http//:www.foo.com', 'www.foo.com');
foreach (@urls)
{
s/^.*?www/www/;
print "http://$_\n";
}
__END__
prints:
http://www.foo.com/
http://www.foo.com
http://www.foo.com
http://www.foo.com
Update: well, this could be more complex as a valid URL does not have to start with www, it could be xyz.tv, then I guess you would want: http://xyz.tv? It helps if you present a representative set of test cases.
It also helps if you can say something about the context of the application. Here I suppose you are trying to "guess" the user's intention of a manually entered URL? And then auto-magically "fix" it? Sometimes it is better to just try to use what the user entered and if it doesn't work, present an error message about what is acceptable for a URL.
Just another regex example... I'm sure that other monks can provide even better regex'es, but specifying the problem as clearly as you can is important.
my @urls = ('tp://www.foo.com/' , '://www.foo.com',
'http//:www.foo.com', 'www.foo.com',
'xxx.tv', 'http//:xxx.tv', 'tp:xx.tv');
foreach (@urls)
{
s/^(.*?)(\w+\.)/$2/;
print "http://$_\n";
}
__END__
prints:
http://www.foo.com/
http://www.foo.com
http://www.foo.com
http://www.foo.com
http://xxx.tv
http://xxx.tv
http://xx.tv
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.