This looks like a job for negative lookahead!

First, have perlre handy for this. Then:

s/\b(?!(?:if|then|else)\b)(\w+)/\$self->\{$1\}\{val\}/g

This code will match only those words you want, and no others.

Note the placement of the \b markers. If you remove the first, you'll end up matching "hen" if your string includes "then" (and similarly for "f" and "lse"). If you remove the second, you'll end up also excluding words that begin with "if", "then", and "else".

But wait, there's more! You can also call perl code from inside the middle of your regular expression and use that to determine whether to match or not. Note that though this is more extensible, the negative lookahead solution is likely much faster.

my %stopwords = map {$_ => qr/\B/} qw(if then else); s/\b(\w+)\b(??{ $stopwords{$^N} or qr[] })/\$self->\{$1\}\{val\}/g

What this means is that after each word is found, perl will look up the word in the %stopwords hash and, if found, will try to match qr/\B/ which can't match at the end of a word. (If the word isn't found in %stopwords, perl tries to match qr// which matches everywhere)

Since this is arbitrary code in the (??{ ... }) block, you can do whatever you want, and aren't limited to a fixed set of stopwords. Just remember to return a regular expression that matches or doesn't match depending on what you need at that point.

--
@/=map{[/./g]}qw/.h_nJ Xapou cets krht ele_ r_ra/; map{y/X_/\n /;print}map{pop@$_}@/for@/

In reply to Re: How to not match some words but match all the others by fizbin
in thread How to not match some words but match all the others by set_uk

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.