Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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

In reply to Re: What is missing from the beginning of this string? by Marshall
in thread What is missing from the beginning of this string? by japhy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (2)
    As of 2020-08-14 00:29 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Which rocket would you take to Mars?










      Results (75 votes). Check out past polls.

      Notices?