Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Captuerd Text as Variable in Search-Operation

Reply
Thread Tools

Captuerd Text as Variable in Search-Operation

 
 
pk
Guest
Posts: n/a
 
      01-12-2004
Hi!

If I do a search an have parenthesis in the search string, I've got
the "captuered" text in the special variables $1, $2 ... My problem
is: I want to pass the search + replace-text as variables and work
with the "captured" text (some user enters the search- +
replace-text); for exampel like that:

$serach_string = "(.*)XXX(.*);
$replace_string = "$1---$1";

$line =~ /$serach_string/;
$new_line = $replace_string;

If $line would be aaaXXXbbb , $new_line should be aaa---bbb .

But I can't use $1 etc. Is there any possibility to get the "captured"
text?

pk
 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      01-12-2004
In article <(E-Mail Removed)> ,
pk <(E-Mail Removed)> wrote:
:If I do a search an have parenthesis in the search string, I've got
:the "captuered" text in the special variables $1, $2 ... My problem
:is: I want to pass the search + replace-text as variables and work
:with the "captured" text (some user enters the search- +
:replace-text); for exampel like that:

:$serach_string = "(.*)XXX(.*);
:$replace_string = "$1---$1";

If the user has control over these expressions, you are going to
have to be careful, as the user might put in ${text} to cause
that text to be executed by you.

:$line =~ /$serach_string/;
:$new_line = $replace_string;

:If $line would be aaaXXXbbb , $new_line should be aaa---bbb .

Not unless the $replace_string were '$1---$2' instead.


:But I can't use $1 etc. Is there any possibility to get the "captured"
:text?

I do not know why you say you cannot use $1 etc. but you do not
seem to need to.

$line =~ s/$search_string/$replace_string/;

But look back to what you had earlier:

$replace_string = "$1---$1";

For the code I gave to work, you would instead need to use:

$replace_string = '$1---$2';

Notice the single-quotes instead of the double-quotes. When you have
the double-quotes, the $1 and $2 will be interpolated at the time
the variable is set. You want the $1 and $2 to be treated literally
until they are encountered in the context of the s/// .
--
Tenser, said the Tensor.
Tenser, said the Tensor.
Tension, apprehension,
And dissension have begun. -- Alfred Bester (tDM)
 
Reply With Quote
 
 
 
 
gnari
Guest
Posts: n/a
 
      01-12-2004
"pk" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) m...
>
> If I do a search an have parenthesis in the search string, I've got
> the "captuered" text in the special variables $1, $2 ... My problem
> is: I want to pass the search + replace-text as variables and work
> with the "captured" text (some user enters the search- +
> replace-text); for exampel like that:
>
> $serach_string = "(.*)XXX(.*);
> $replace_string = "$1---$1";
>
> $line =~ /$serach_string/;
> $new_line = $replace_string;
>
> If $line would be aaaXXXbbb , $new_line should be aaa---bbb .


actually it would be aaa---aaa

>
> But I can't use $1 etc. Is there any possibility to get the "captured"
> text?


take a look at the /e modifier of s///
perdoc perlop
you can do what you want with /ee
but you have to think about what you are doing
s/$serach_string/'"'.$replace_string.'"'/ee;

you should ABSOLUTELY NOT do this if
$serach_string is not trusted.

gnari




 
Reply With Quote
 
gnari
Guest
Posts: n/a
 
      01-12-2004
"gnari" <(E-Mail Removed)> wrote in message
news:btupie$jqc$(E-Mail Removed)...
> "pk" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) m...
> >

[snipped regexp problem w/ variable replace w/ captures]

[snipped solution involving /ee]

I regret having posted the /ee solution.

here is a solution that is safer:

$serach_string = '(.*)XXX(.*)';
$replace_string = '$1---$2';
$line='aaaXXXbbb';

my @cap=(undef,/$serach_string/);
(my $r=$replace_string)=~s/\$(\d)/$cap[$1]/g;
$line=~s/$serach_string/$r/g;





 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      01-12-2004

"gnari" <(E-Mail Removed)> wrote:
> here is a solution that is safer:


You would need to wrap both matches in eval{} to catch the possible
'Eval-group not allowed at runtime' error.

> $serach_string = '(.*)XXX(.*)';
> $replace_string = '$1---$2';
> $line='aaaXXXbbb';
>
> my @cap=(undef,/$serach_string/);


ITYM my @cap = (undef, $line =~ /$serach_string/);

> (my $r=$replace_string)=~s/\$(\d)/$cap[$1]/g;


I would have \d+ here...?

> $line=~s/$serach_string/$r/g;


Ben

--
And if you wanna make sense / Whatcha looking at me for? (Fiona Apple)
* http://www.velocityreviews.com/forums/(E-Mail Removed) *
 
Reply With Quote
 
gnari
Guest
Posts: n/a
 
      01-12-2004
"Ben Morrow" <(E-Mail Removed)> wrote in message
news:btut2v$bi0$(E-Mail Removed)...
>
> "gnari" <(E-Mail Removed)> wrote:
> > here is a solution that is safer:

>
> You would need to wrap both matches in eval{} to catch the possible
> 'Eval-group not allowed at runtime' error.
>
> > $serach_string = '(.*)XXX(.*)';
> > $replace_string = '$1---$2';
> > $line='aaaXXXbbb';
> >
> > my @cap=(undef,/$serach_string/);

>
> ITYM my @cap = (undef, $line =~ /$serach_string/);


yep. I started with $_ but changed to $line
to conform with the original post and missed this one

>
> > (my $r=$replace_string)=~s/\$(\d)/$cap[$1]/g;

>
> I would have \d+ here...?


I was wondering if someone would mention this

gnari



 
Reply With Quote
 
Robin
Guest
Posts: n/a
 
      01-13-2004
> If I do a search an have parenthesis in the search string, I've got
> the "captuered" text in the special variables $1, $2 ... My problem
> is: I want to pass the search + replace-text as variables and work
> with the "captured" text (some user enters the search- +
> replace-text); for exampel like that:
>
> $serach_string = "(.*)XXX(.*);


for one thing, this doesn't have it's quote at the end...

> $replace_string = "$1---$1";
>
> $line =~ /$serach_string/;
> $new_line = $replace_string;
>
> If $line would be aaaXXXbbb , $new_line should be aaa---bbb .
>
> But I can't use $1 etc. Is there any possibility to get the "captured"
> text?
>
> pk


--
peace,
Robin
--------------
(E-Mail Removed)
Please CC a copy of your message to me.
--------------




 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      01-13-2004
[ posted and accidently mailed also - oops ]

(E-Mail Removed) (pk) writes:

> If I do a search an have parenthesis in the search string, I've got
> the "captuered" text in the special variables $1, $2 ... My problem
> is: I want to pass the search + replace-text as variables and work
> with the "captured" text (some user enters the search- +
> replace-text); for exampel like that:
>
> $serach_string = "(.*)XXX(.*);
> $replace_string = "$1---$1";
>
> $line =~ /$serach_string/;
> $new_line = $replace_string;
>
> If $line would be aaaXXXbbb , $new_line should be aaa---bbb .
>
> But I can't use $1 etc. Is there any possibility to get the "captured"
> text?


Y'know, this is the most frequently asked single question not
currently in the Perl FAQ.

In some senses it's really just a special case of the existing FAQ
"How can I expand variables in text strings?" but the s/// question
actually appears more often the the general case so I think it
probably warrants its own entry. Anyhow I've always disliked the
answer given in the FAQ to "How can I expand variables in text
strings?" because I think it's incomplete.

Anyhow for more suggestions than you could shake a stick at see dozens
(hundreds?) previous threads asking the exact same question. Sooner
or later in any such thread someone is pretty-much sure to mention the
FAQ "How can I expand variables in text strings?" so searching on that
phrase should pick up pretty-much all such threads.

--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      01-13-2004
gnari <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> "gnari" <(E-Mail Removed)> wrote in message
> news:btupie$jqc$(E-Mail Removed)...
> > "pk" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed) m...
> > >

> [snipped regexp problem w/ variable replace w/ captures]
>
> [snipped solution involving /ee]
>
> I regret having posted the /ee solution.
>
> here is a solution that is safer:
>
> $serach_string = '(.*)XXX(.*)';
> $replace_string = '$1---$2';
> $line='aaaXXXbbb';
>
> my @cap=(undef,/$serach_string/);


Should be

my @cap=(undef,$line =~ /$search_string/);

as has been noted.

> (my $r=$replace_string)=~s/\$(\d)/$cap[$1]/g;
> $line=~s/$serach_string/$r/g;


That does the job, but I find it a little hard to see how it does it.
This may be more readable:

# keep whatever the user's pattern captures (to put it back in later)
my @stick_back_in = $line =~ /$search_string/;

#do the replacement literally
$line =~ s/$search_string/$replace_string;

# now $line has literal "$1", "$2", etc. in it. Replace them
# one by one with the original text they captured
$line =~ s/\$\d+/shift @stick_back_in/eg;

Anno
 
Reply With Quote
 
pk
Guest
Posts: n/a
 
      01-13-2004
"Robin" <(E-Mail Removed)> wrote in message news:<btvfeo$chh$(E-Mail Removed)>...
> > If I do a search an have parenthesis in the search string, I've got
> > the "captuered" text in the special variables $1, $2 ... My problem
> > is: I want to pass the search + replace-text as variables and work
> > with the "captured" text (some user enters the search- +
> > replace-text); for exampel like that:
> >
> > $serach_string = "(.*)XXX(.*);

>
> for one thing, this doesn't have it's quote at the end...
>
> > $replace_string = "$1---$1";
> >
> > $line =~ /$serach_string/;
> > $new_line = $replace_string;
> >
> > If $line would be aaaXXXbbb , $new_line should be aaa---bbb .
> >
> > But I can't use $1 etc. Is there any possibility to get the "captured"
> > text?
> >
> > pk


You are right - thanks! But thats not the problem ...

pk
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Please help me how is easiest way to extract text between some variable text Mladen Perl Misc 5 02-22-2011 10:57 AM
"Variable variable name" or "variable lvalue" mfglinux Python 11 09-12-2007 03:08 AM
Controlling text in a Text Area or Text leo ASP General 1 12-05-2005 01:13 AM
Convert Character Variable to Integer Variable Brad Smallridge VHDL 2 11-18-2004 01:56 AM
How do I scope a variable if the variable name contains a variable? David Filmer Perl Misc 19 05-21-2004 03:55 PM



Advertisments