$texttosub =~ s/(?!log10)|([a-zA-Z][A-Za-z_0-9]+)/$cats{$1}/i
But I am getting an infinite loop for some reason.

You don't need the alternation operator |. Lookaround Assertions like (?!...) are zero-width (tutorial). So the regex / (?!foo) | bar /x means "match a zero-length string as long as the next thing isn't foo, or match bar".

However, even with the alternation operator removed, the regex still won't do what you expect: the pattern basically means "any letters or numbers, as long as the next thing isn't log10", so the result will be "l(5)", because the substring og10 matches the pattern and there is no key og10 in %cats!

Although I've already said that I agree with Corion that using a proper parser is better, and I've shown a different solution, just for the sake of completeness and TIMTOWTDI, here are two additional ways to do what you want. First, you can use the word boundary \b to make sure that the string being matched isn't just a portion of a longer identifier: s/(?!log10)(\b[a-zA-Z][A-Za-z_0-9]+)/$cats{$1}/ works on the sample input you've shown.

Second, you could look at all identifiers, and then figure out what they are once you've matched them. Here, I'm taking advantage of the /e modifier to execute the replacement part as Perl code:

use warnings; use strict; my %cats = (blackcat=>5, whitecat=>10,orangecat=>20); my %funcs = map {$_=>1} qw/ log10 sin cos /; # etc. my $texttosub = "log10(blackcat)*whitecat*(log10(orangecat))"; $texttosub =~ s{(\b[a-zA-Z][a-zA-Z0-9_]+\b)}{ my $repl; if ($cats{$1}) { $repl = $cats{$1} } elsif ($funcs{$1}) { $repl = $1 } else { die "Unknown identifier '$1'" } print "Matched '$1', replacement '$repl'\n"; # Debug $repl }eg; print $texttosub, "\n"; __END__ Matched 'log10', replacement 'log10' Matched 'blackcat', replacement '5' Matched 'whitecat', replacement '10' Matched 'log10', replacement 'log10' Matched 'orangecat', replacement '20' log10(5)*10*(log10(20))

In reply to Re: Is there any way to ignore certain words when substituing? by haukex
in thread Is there any way to ignore certain words and keep it as it is when substituing hash values to a matched pattern in a string? by skooma

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.