Ask a Question related to PERL Miscellaneous, Design and Development.
-
Mithras #1
s/$pattern_to_match/$pattern_to_replace/ -- how can i use matched expressions?
Hi all,
I want to use the substitution operator with variables rather than
string literals -- no problem, right?
My problem comes when I try to use the $1, $2, etc. variables to refer
to the matched expressions.
For example:
$pattern_to_match = 'I am (.*)';
$pattern_to_replace = 'You are $1';
$line =~ s/$pattern_to_match/$pattern_to_replace/g;
For example, if $line contains 'I am green', I want it to be replace
with 'You are green'.
However, the code fragment above would result in 'You are ' -- the $1
variable doesn't work as the match substitution...
Can anyone help me out?
I realize I may be able to work something out with the s///e option,
but this looks a little too complex for what I want to do.
TIA
Mithras Guest
-
#38860 [Fbk->Opn]: preg_match returns wrong positions of matched substrings when UTF-8 is used
ID: 38860 User updated by: me at andr dot biz Reported By: me at andr dot biz -Status: Feedback +Status: ... -
DRWM8: How to remove underline from Matched text?
How to remove underline from Matched text in search result? Is there any extension to do so? Thanx a lot PEPP -
Keeping Grid Checkbox Control matched with correct row
Greetings, I have an issue that I am not seeing a blatant solution for. My DataGrid is an "Accept/Reject" type grid. So there is a check box in... -
How do I<STDIN> to a stack until a pattern is matched, How do I
Is there an easy way to read <STDIN> into a stack until some pattern is matched and then break. I tried all sorts of (error producing) code, but... -
How do I<STDIN> to a stack until a pattern is matched, How do I
> How can I do this? You can do it like this... # tested my @stack; while (<STDIN>) { last if /QUIT/; push @stack, $_; -
Jay Tilton #2
Re: s/$pattern_to_match/$pattern_to_replace/ -- how can i use matched expressions?
[email]mybulkmail@myrealbox.com[/email] (Mithras) wrote:
: I want to use the substitution operator with variables rather than
: string literals -- no problem, right?
:
: My problem comes when I try to use the $1, $2, etc. variables to refer
: to the matched expressions.
: For example:
:
: $pattern_to_match = 'I am (.*)';
: $pattern_to_replace = 'You are $1';
Don't let yourself call the replacement string a "pattern."
Badly named variables will only bring confusion.
: $line =~ s/$pattern_to_match/$pattern_to_replace/g;
:
: For example, if $line contains 'I am green', I want it to be replace
: with 'You are green'.
: However, the code fragment above would result in 'You are ' -- the $1
: variable doesn't work as the match substitution...
:
: Can anyone help me out?
: I realize I may be able to work something out with the s///e option,
: but this looks a little too complex for what I want to do.
Not all that complex.
my $pattern_to_match = 'I am (.*)';
my $string_to_replace = '"You are $1"';
my $line = 'I am green';
$line =~ s/$pattern_to_match/$string_to_replace/eeg;
print $line;
Jay Tilton Guest



Reply With Quote

