s/A/B/ and s/B/C/ but don't want A -> C (was: FAQ 6.4 I put a regular expression into $/ but it didn't work. What's wrong?)



On Thu, 27 Dec 2007 21:35:11 +0000 Ben Morrow <ben@xxxxxxxxxxxx> wrote:

BM> Quoth PerlFAQ Server <brian@xxxxxxxxxxxxxx>:

6.4: I put a regular expression into $/ but it didn't work. What's wrong?

Up to Perl 5.8.0, $/ has to be a string. This may change in 5.10, but
don't get your hopes up. Until then, you can use these examples if you

BM> s/8/10/; s/10/12/;

You want that backwards :)

Actually, I've often run into the need for parallel edits like this,
where you want to s/A/B/ and s/B/C but you don't want A to become C.
With complex operations or operations you don't know in advance, proper
ordering becomes impossible. I usually handle it with function calls:
s/[ABC]/replacement(\1)/e or something like that. Is there a better
approach anyone can recommend?

Thanks
Ted
.



Relevant Pages

  • Re: Parsing a string into an array
    ... pragmas to let perl help you find mistakes. ... split Splits a string into a list of strings and returns ... The first argument to splitis a regular expression pattern. ...
    (perl.beginners)
  • Re: Metacharacter problem in regexp
    ... wrong with regular expressions in Perl 5 and how are they going to look ... interpolation you've asked about and the klunky workaround I've sent ... any piece of program is a string. ... string as a regular expression. ...
    (perl.beginners)
  • Re: regular expression
    ... > I am looking for help on a PERL regular expression that can do the ... Make sure that the string is 20 chars or else match should fail. ... The string can have word characters or spaces or comma, hyphen, ... > Any ideas on how this can be done using just one regular expression? ...
    (perl.beginners)
  • Re: A way of checking if a string contains a number
    ... I'm new to python, but I have used a fair bit of C and Perl ... All I am trying to do is detect if there is a number in a string. ... Is there a way I can drop the regular expression module and just use ...
    (comp.lang.python)
  • Re: Splitting on =
    ... >>> How do you split a string on the equality character? ... > a single string. ... it treats the first argument as a regular expression ... use Perl; ...
    (perl.beginners)