473,246 Members | 1,835 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,246 software developers and data experts.

RegExp Replace Using a Variable

Hi all:

I'm trying to replace using a regexp read from a file:

$string = '123 456 789';
# these two are usualy read from a file,
# and so have to be in variables.
$re = '([258])';
$rep = '|$1|';

$string =~ s/$re/$rep/g;

I would like $string to be: 1|2|3 4|5|6 7|8|9
but it is: 1|$1|3 4|$1|6 7|$1|9

I understand why this is happening, but how do I make
it do what I want?

I've been reading around, I found an answer that almost works:
$string = '123 456 789';
# these two are usualy read from a file
$re = '([258])';
$rep = sub { "|$1|" };

$string =~ s/$re/$rep->()/ge;

This works, but it's not what I want. I want:
$string = '123 456 789';
# these two are usualy read from a file
$re = '([258])';
$repVar = '|$1|' # or "|$1|" doesn't work either way
$rep = sub { $repVar };

$string =~ s/$re/$rep->()/ge;

But this doesn't work.

Any help would be appritieted.
Thanks.
Jul 19 '05 #1
5 14122

Ones Self wrote:
I'm trying to replace using a regexp read from a file:

$string = '123 456 789';
$re = '([258])';
$rep = '|$1|';

$string =~ s/$re/$rep/g;

I would like $string to be: 1|2|3 4|5|6 7|8|9
but it is: 1|$1|3 4|$1|6 7|$1|9

$string = '123 456 789';
# these two are usualy read from a file,
# and so have to be in variables.
$re = '([258])';
$rep = '|$1|';

$doit="\$string=~s/$re/$rep/g";
eval "$doit";

print $string;

--
_____cliff_rayman_____cliff_@_rayman_._com_____

Jul 19 '05 #2
___cliff rayman___ <cl***@rayman.com> wrote in message news:<78****************@twister.socal.rr.com>...
Ones Self wrote:
I'm trying to replace using a regexp read from a file:

$string = '123 456 789';
$re = '([258])';
$rep = '|$1|';

$string =~ s/$re/$rep/g;

I would like $string to be: 1|2|3 4|5|6 7|8|9
but it is: 1|$1|3 4|$1|6 7|$1|9

$string = '123 456 789';
# these two are usualy read from a file,
# and so have to be in variables.
$re = '([258])';
$rep = '|$1|';

$doit="\$string=~s/$re/$rep/g";
eval "$doit";

print $string;


I appologize for neglecting to mention this in my original post,
but this is actually what I'm currently using. However, since
I'm dealing with a _lot_ of records (~1.5M lines), eval is
really slow (about 8 times slower than a static regexp).
Furthermore, if there was a way to compile the regexp
that would be very nice.

Thanks
Jul 19 '05 #3

Ones Self wrote:
___cliff rayman___ <cl***@rayman.com> wrote in message news:<78****************@twister.socal.rr.com>...

Ones Self wrote:

I'm trying to replace using a regexp read from a file:

$string = '123 456 789';
$re = '([258])';
$rep = '|$1|';

$string =~ s/$re/$rep/g;

I would like $string to be: 1|2|3 4|5|6 7|8|9
but it is: 1|$1|3 4|$1|6 7|$1|9


$string = '123 456 789';
$re = '([258])';
$rep = '|$1|';

$doit="\$string=~s/$re/$rep/g";
eval "$doit";

print $string;

I'm dealing with a _lot_ of records (~1.5M lines), eval is
really slow (about 8 times slower than a static regexp).

OK - I am not sure how to get around this problem using regex commands.
Maybe a guru would know of one of the top of their head. If it were me,
I would generate a little program from perl, then execute it. It would
use the regex's from the file, and a template to generate a perl
executable program to perform the actual work.

--
_____cliff_rayman_____cliff_@_rayman_._com_____

Jul 19 '05 #4
jan
___cliff rayman___ <cl***@rayman.com> wrote in message news:<GM*****************@twister.socal.rr.com>...
Ones Self wrote:
___cliff rayman___ <cl***@rayman.com> wrote in message news:<78****************@twister.socal.rr.com>...
Ones Self wrote:

I'm trying to replace using a regexp read from a file:

$string = '123 456 789';
$re = '([258])';
$rep = '|$1|';

$string =~ s/$re/$rep/g;

I would like $string to be: 1|2|3 4|5|6 7|8|9
but it is: 1|$1|3 4|$1|6 7|$1|9
$string = '123 456 789';
$re = '([258])';
$rep = '|$1|';

$doit="\$string=~s/$re/$rep/g";
eval "$doit";

print $string;

I'm dealing with a _lot_ of records (~1.5M lines), eval is
really slow (about 8 times slower than a static regexp).

OK - I am not sure how to get around this problem using regex commands.
Maybe a guru would know of one of the top of their head. If it were me,
I would generate a little program from perl, then execute it. It would
use the regex's from the file, and a template to generate a perl
executable program to perform the actual work.


Your problem is that $1 has no value at the time that you are
assigning it's contents to $rep. This should do what you want:

$string = "123 456 789";
$re = "([258])";
$string =~ /$re/g;
$foo = $1;
$rep = "|".$foo."|";
$string =~ s/$re/$rep/og;

print $string, "\n";
Cheers,
Jan
Jul 19 '05 #5
Ones Self wrote:
I'm trying to replace using a regexp read from a file:

$string = '123 456 789';
# these two are usualy read from a file,
# and so have to be in variables.
$re = '([258])';
$rep = '|$1|';

$string =~ s/$re/$rep/g;

I would like $string to be: 1|2|3 4|5|6 7|8|9
but it is: 1|$1|3 4|$1|6 7|$1|9


You could for instance create and use a hash:

my $string = '123 456 789';
my $re = qr/([258])/; # compiles $re
my $rep = '|$1|';

my @rep = $rep =~ /(.*)\$1(.*)/;
my %rep;
$rep{$1} = "$rep[0]$1$rep[1]" while $re =~ /(\d)/g;

$string =~ s/$re/$rep{$1}/g;

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Jul 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Ian Renfrew | last post by:
To replace characters, I currently run the following code: oStr = oStr.replace(/\xFC/g,"\t"); // replace sub-value mark(s) with tab(s) Ideally, I'd like to replace the hard coded string \xFC...
1
by: eysteinbye | last post by:
I have a problem with my RegExp. I need to replace some URLs with other URLs. I am using a URL-rewrite filter). The problem is that when the RegExp match just parts of the string, it does a...
4
by: Jon Maz | last post by:
Hi All, I want to strip the accents off characters in a string so that, for example, the (Spanish) word "práctico" comes out as "practico" - but ignoring case, so that "PRÁCTICO" comes out as...
13
by: Phat G5 (G3) | last post by:
I am weak when it comes to regexp but hoped someone might know in this case. I am trying to take a url like this : something.lasso?blah=blah&blah2=blah2&sort=hello&blah3=blah3 And remove the...
7
by: Csaba Gabor | last post by:
I need to come up with a function function regExpPos (text, re, parenNum) { ... } that will return the position within text of RegExp.$parenNum if there is a match, and -1 otherwise. For...
8
by: reflex | last post by:
Hi, i have script with pattern (href=?(.*)({1}+)?), where i match any occurence of url and replace hyperlink so i have only anchor in it. var regExp = /href=?(.*)({1}+)?/ig; var wholeContent...
3
by: yoni | last post by:
Hi, I am trying to write a regexp to find all the code on the header of entities in SQL Server (views, SPs, etc...) I got something like this: (.|\n)*((create view)|(create proc)|(create...
2
by: X l e c t r i c | last post by:
Here: http://bigbangfodder.fileave.com/res/sandr.html I'm trying to use string.replace() for a basic search and replace form using textarea values as the regexp and replacement values for...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.