Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
join "|", map { "\\b$_\\b" } reverse sort keys %cats;

I'd suggest putting a quotemeta in there, just to play it safe. Although I agree that using a proper parser is better! skooma: See also Building Regex Alternations Dynamically.

use warnings; use strict; use Test::More; my %cats = (blackcat=>5, whitecat=>10,orangecat=>20); my ($regex) = map { qr/\b($_)\b/ } join '|', map {quotemeta} sort { length $b <=> length $a or $a cmp $b } keys %cats; diag explain $regex; sub do_replace { my $input = shift; $input =~ s/$regex/$cats{$1}/g; return $input; } is do_replace("log10(blackcat)"), "log10(5)"; is do_replace("log10(blackcat)*whitecat*(log10(orangecat))"), "log10(5)*10*(log10(20))"; done_testing; __END__ # qr/\b(orangecat|blackcat|whitecat)\b/ ok 1 ok 2 1..2

In reply to Re^2: 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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2024-04-19 10:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found