Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Dynamic data within regex pattern?

Reply
Thread Tools

Dynamic data within regex pattern?

 
 
DM
Guest
Posts: n/a
 
      10-01-2004
Is there a way to set up a regex substitution so that the pattern is dynamically
generated at runtime?

I.e., something along the lines of...

$output =~ s/$someVar/$someOtherVar/g;

I read in my Perl book that the replacement string is variable interpolated, but
it didn't say anything about that being the case for the regex pattern. So I
guess in the situation above, the regex would match the end of a line followed
by "someVar". And if you escaped the dollar sign, it would match "$someVar"
litterally.

Any assistance would be greatly appreciated.
 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      10-01-2004
DM wrote:
> Is there a way to set up a regex substitution so that the pattern
> is dynamically generated at runtime?
>
> I.e., something along the lines of...
>
> $output =~ s/$someVar/$someOtherVar/g;


What happened when you tried it?

> I read in my Perl book that the replacement string is variable
> interpolated, but it didn't say anything about that being the case
> for the regex pattern. So I guess in the situation above, the regex
> would match the end of a line followed by "someVar".


You guessed wrong.

> Any assistance would be greatly appreciated.


You don't need any assistance at this stage. You need to play with
Perl, and figure out what 'works' and what doesn't. And maybe read a
little more.

Come back here if you fail in solving a particular problem.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      10-01-2004
DM wrote:

> Is there a way to set up a regex substitution so that the pattern is
> dynamically generated at runtime?
>
> I.e., something along the lines of...
>
> $output =~ s/$someVar/$someOtherVar/g;
>
> I read in my Perl book that the replacement string is variable
> interpolated, but it didn't say anything about that being the case for
> the regex pattern.


What Perl book was that, may I ask?

> So I guess in the situation above, the regex would
> match the end of a line followed by "someVar".


Why just guess? Why not actually try it?

> And if you escaped the
> dollar sign, it would match "$someVar" litterally.
>
> Any assistance would be greatly appreciated.


Read the section "Regexp Quote-Like Operators" from perldoc perlop.
Specifically, the paragraph beginning "PATTERN may contain variables,"

Paul Lalli
 
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
How make regex that means "contains regex#1 but NOT regex#2" ?? seberino@spawar.navy.mil Python 3 07-01-2008 03:06 PM
regex bug (comments within regex not as robust) kg.google@olympiakos.com Perl Misc 3 10-27-2005 07:21 PM
Is ASP Validator Regex Engine Same As VS2003 Find Regex Engine? =?Utf-8?B?SmViQnVzaGVsbA==?= ASP .Net 2 10-22-2005 02:43 PM
Java regex imposture re: Perl regex compatibility a_c_Attlee@yahoo.com Java 2 05-06-2005 12:16 AM
perl regex to java regex Rick Venter Java 5 11-06-2003 10:55 AM



Advertisments