Well it works but I'm clearly way out-monked here because I don't have a clue how! Can someone enlighten me?

The trick lies in the zero-width assertion: It will fail if none of the following optionals match anything. (It asserts no rice, but if none of the optionals match, rice is what follows.)

I guess my answer was rather on the minimalist side. So, here is a commented version. But I won't just comment the same version I made first -- what's the fun in that? This time I'll use a positive lookahead zero-width assertion, not a negative. And I'll borrow the variables from tachyon's answer, including the job description that I somehow forgot first time around.

So, here goes:

# Note, tachyon, we need the /i modifier on each of these as well: my $job_desc = qr/\b national\s+security\s+adviser \s+/ix; my $title = qr/\b (?:dr\.|doctor) \s+/ix; my $f_name = qr/\b condoleeza \s+/ix; my $surname = qr/\b rice \b/ix; my $repl = 'condoleezarice'; $text =~ s/# Zero-width assertion: One of the options must follow: (?= $job_desc | $title | $f_name ) $job_desc ? # Option 1: job description $title ? # Option 2: title $f_name ? # Option 3: first name $surname # Surname -- not optional /$repl/gx;

(If the surname could match any of the optionals, this would not work. Neither would the negative lookahead, though it would fail differently. But that is a problem I'll face no sooner than I need to.)

Update: Turns out there is an implied grouping with qr//, so I have removed the explicit grouping from my code.

print "Just another Perl ${\(trickster and hacker)},"
The Sidhekin proves Sidhe did it!


In reply to Re^3: Non-exclusive or in regexp? by Sidhekin
in thread Non-exclusive or in regexp? by cormanaz

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, details, 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, summary, 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.