greenhorn has asked for the wisdom of the Perl Monks concerning the following question:
I thought I had some proficiency with regular expressions...until I met perl...
The goal was a substitution in which literal square brackets were to be part of the replacement text and in which certain variables would be used in the replacement as well. I had assumed that the brackets would be treated as literals. Wrong. Following are examples of the kinds of results I got:
$s = 'now is the time for all good etc.;' $bef = 'before'; $mid = 'middle'; $aft = 'after'; Regexp: result (was it expected?) s/.+/$bef$mid$aft/: beforemiddleafter (expected result, if no brackets) s/.+/$bef[$mid$aft/: (SYNTAX ERROR: "scalar found where operator expected"...) s/.+/[$bef]$mid$aft/: [before]middleafter (expected) s/.+/$bef[$mid]$aft/: after (huh?) s/.+/$bef$mid[$aft]/: before (whaa?)
So much for proficiency. I had been aware that not every possible
sort of text is taken literally on the "replace-with" side of the substitution,
but I was unprepared for the above results. I need to do some more reading.
Which of the sundry books on perl would provide the best information about
what is happening in these kinds of substitutions?
T.I.A. . . .
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
RE: Regexp: not what I'd expected
by Russ (Deacon) on Jul 04, 2000 at 05:47 UTC | |
by greenhorn (Sexton) on Jul 04, 2000 at 06:13 UTC | |
|
Re: Regexp: not what I'd expected
by maverick (Curate) on Jul 04, 2000 at 05:54 UTC | |
by greenhorn (Sexton) on Jul 04, 2000 at 06:32 UTC | |
|
RE: Regexp: not what I'd expected
by jeorgen (Pilgrim) on Jul 04, 2000 at 05:52 UTC | |
|
Re: Regexp: not what I'd expected
by Anonymous Monk on Jul 04, 2000 at 06:51 UTC | |
|
Re: Regexp: not what I'd expected
by Ovid (Cardinal) on Jul 04, 2000 at 21:32 UTC | |
|
Re: Regexp: not what I'd expected
by Anonymous Monk on Jul 04, 2000 at 06:52 UTC |