Hi skasch,

Assuming that difference in the two strings pointed out in the AM post is just a copy/paste mistake and you meant to say "_A_domain_D_com" instead of "_A_albertbauer_D_com", the reason why your code isn't working as intended is \b. This matches the boundary between a word (\w) and non-word (\W) character. \w is discussed in detail here, but to simplify, under ASCII (/a) it's the set [a-zA-Z0-9_]. Note how it includes letters, digits, and the underscore. Therefore, the string user_A_domain_D_com_BCA contains no internal word bounaries, and the regex you are trying to match it against, /\b_A_domain_D_com_\b/ will not match that string.

Having said that, note how in your solution you're having to maintain two sets of regexes: %replacements and the long if statement. While it's possible this is necessary due to the structure of the data (I don't know all your requirements), note that there are ways to express the same stuff without the repetition. Here's one way to dynamically build the regex:

my %replacements = (...); my $regex = join '|', map {quotemeta} sort { length $b <=> length $a } keys %replacements; $regex = qr/$regex/; # ... while ( <$readFile> ) { s/($regex)/$replacements{$1}/g; print $_; }

And if you need to check for the presence of the strings "Calendar" / "Contacts" / "Users", as in your example code, you can combine the multiple regexes into something like /:(?:Calendar|Contacts)(?=\/)|(?<=\"\/)Users/ (Update: shortened regex slightly).

One more thing: Mind the Meta! Note how the %replacements key user.name@domain.com contains two dots - in your current solution, those will match any character. Only if you say \Q$key\E (or manually use quotemeta as I showed above) will the dots and any other special regex chars be matched literally.

Hope this helps,
-- Hauke D


In reply to Re: Cannot get Perl to replace a specific string in my textfile by haukex
in thread Cannot get Perl to replace a specific string in my textfile by skasch

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.