http://www.perlmonks.org?node_id=1012698

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

ntv.com/very-own/ ntv.com/business/ ntv.com.wid-prl.com/very-own/ ltv.com/very-own/
Please tell me how to write a single regex which matches all the above conditions.

Replies are listed 'Best First'.
Re: Single regex
by choroba (Cardinal) on Jan 10, 2013 at 16:54 UTC
    You probably do not want to see
    /.*/
    or
    m{^(?: ntv\.com/very-own/ | ntv\.com/business/ | ntv\.com\.wid-prl\.com/very-own/ | ltv\.com/very-own/ )$}x
    as the answers. Tell us more: What conditions should it not match? What are you trying to achieve?
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Single regex
by BrowserUk (Patriarch) on Jan 10, 2013 at 16:57 UTC

    print "$_: ", m[[nl]tv\.(?:com\.wid-prl\.)?com/(?:business|very-own)/] + ? 'matches' : 'nomatch' for qw[ ntv.com/very-own/ ntv.com/business/ ntv.com.wid-prl.com/very-own/ ltv.com/very-own/ ];; ntv.com/very-own/: matches ntv.com/business/: matches ntv.com.wid-prl.com/very-own/: matches ltv.com/very-own/: matches

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      good code, but I would add ^ and $
      m[^[nl]tv\.(?:com\.wid-prl\.)?com/(?:business|very-own)/$]
Re: Single regex
by clueless newbie (Curate) on Jan 10, 2013 at 18:03 UTC

    Using Regexp::Assemble

    use strict; use warnings; use Regexp::Assemble; my $re=Regexp::Assemble->new(); $re->add('ntv\.com/very-own/'); $re->add('ntv\.com/business/'); $re->add('ntv\.com.wid-prl.com/very-own/'); $re->add('ltv\.com/very-own/'); print $re->re."\n";

    produces

    (?^:(?:ntv\.com(?:\/(?:business|very-own)|.wid-prl.com\/very-own)|ltv\.com\/very-own)\/)
Re: Single regex
by muba (Priest) on Jan 10, 2013 at 16:57 UTC
    m(ntv\.com/(very-own|business)/(ntv\.com\.wid-prl\.com|ltv\.com)/very-own/)
Re: Single regex
by 0day (Sexton) on Jan 10, 2013 at 17:01 UTC
    /^*tv\.com.+?/si
Re: Single regex
by Anonymous Monk on Jan 11, 2013 at 02:37 UTC