Without punctuation and string delimiters, it's very difficult to know exactly what you're asking. The guidelines in "How do I post a question effectively?" will help you post a better question.

Regex substitution is performed with s///. You should probably read "perlrequick - Perl regular expressions quick start" and "perlretut - Perl regular expressions tutorial" to get a basic understanding of Perl's regular expressions before doing anything else.

It sounds like you probably want a lookup table and a regex using alternation. Here's a very simplistic example of the technique:

#!/usr/bin/env perl -l use strict; use warnings; my %lookup = ('500' => '000', '0 988' => '000', '0 1210' => '1000'); my $re = '(' . join('|' => keys %lookup) . ')'; my $string = 'www "999" xxx "0 1210" yyy "500" zzz "0 988"'; print $string; $string =~ s/$re/$lookup{$1}/g; print $string;

Output:

www "999" xxx "0 1210" yyy "500" zzz "0 988" www "999" xxx "1000" yyy "000" zzz "000"

Depending on the context of the strings to be replaced, the pattern may need to be a lot more complex than that shown. For instance, given '500 988', do you want to replace 500' and ignore ' 988' or ignore '50' and replace '0 988'. Reading the documentation I linked to will help you understand these issues and how to deal with them. And, of course, assuming '0 988' is a string to be replaced is just a guess on my part (as I alluded to in the opening paragraph).

-- Ken


In reply to Re: String regex replacement by kcott
in thread String regex replacement by Anonymous Monk

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.