CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
replacing part of a string with perl code embedded in regex

 



supenguin
New User

May 29, 2008, 2:42 PM

Post #1 of 4 (3041 views)
replacing part of a string with perl code embedded in regex Can't Post

I've got a bit of code that should be simple in a regex, but just can't seem to get it to work. In short, we've got some data stored in the database that looks like "string1&2" and want to split this out to be "string1" in one variable & "string2" in another. The numbers are always on the end of the strings, but they are not always one digit. It can be anywhere from 1 to 3 digits. In one case we have "string041&42" so this would map to "string041" and "string042".

The code looks something like this:

my @strings = split("&", $strings);

$string1 = $strings[0];

$string2 = $strings[0];

$string2 =~ s/\d{length($strings[1])}$/$strings[1]/;

Doing this doesn't work out. I always end up with $string2 being the same as $string1.

I'm sure there is some way to do this with regular expressions, I just can't quite figure it out. I have ended up doing it another way for now, but it isn't as nice as a 1 liner regex would be. I count the characters I need to replace in $string2, chop them off the end, and then tack $strings[1] onto the end.


KevinR
Veteran


May 29, 2008, 5:14 PM

Post #2 of 4 (3037 views)
Re: [supenguin] replacing part of a string with perl code embedded in regex [In reply to] Can't Post

I think your assumption is wrong. It will not be a simple regexp. First problem is that you can't use perl functions in the search side of a substitution regexp:

s/search side/substitution side/

You will need to use more than a single regexp or use some other methods although this does sound like a job for regular expressions.

One way:


Code
use warnings; 
use strict;
my $strings = 'string041&42';
my ($str1,$nums1,$nums2) = $strings =~ /(\D*)(\d*)&(\d*)/;
my $str2;
my( $L1, $L2) = (length $nums1, length $nums2);

if ($L1 == $L2) {
$str2 = "$str1$nums2";
}
elsif ($L1 > $L2) {
my $r = substr $nums1 , 0, $L1 - $L2;
$str2 = "$str1$r$nums2";
}
else {
print "Don't know what else";
}
my $str1 = "$str1$nums1";
print "$str1 , $str2";


Or maybe a clever person will come up with a simpler solution.
-------------------------------------------------


supenguin
New User

May 30, 2008, 5:43 AM

Post #3 of 4 (3018 views)
Re: [supenguin] replacing part of a string with perl code embedded in regex [In reply to] Can't Post

Here's what I did yesterday that ended up working.


Code
my @strings = split("&", $string); 
if ($strings[1] !~ /\D/) {
my $string_1 = $strings[0];
my $string_2 = $strings[0];
for (my $c = 0; $c < length($strings[1]); $c++) {
chop($string_2);
}
$string_2 = $string_2 . $strings[1];
$strings[0] = $string_1;
$strings[1] = $string_2;
}



KevinR
Veteran


May 30, 2008, 11:56 AM

Post #4 of 4 (3006 views)
Re: [supenguin] replacing part of a string with perl code embedded in regex [In reply to] Can't Post

OK, that got me to thinking a little bit moe and here is another suggestion:


Code
use warnings;  
use strict;
my $string = 'string041&42';
my @strings = split("&", $string);
my( $L1, $L2) = (length $strings[0], length $strings[1]);
my $new = $strings[0];
substr $strings[0], -$L2, $L2, $strings[1];
print "$new, $strings[0]";


This sould be very efficient, but your way is OK too. The amount of data involved is probably so little it really does not matter how you do it. If you are crunching through millions of strings to parse them into two strings it might matter, but for a few hundred or a few thousand the difference is not going to be overly dramatic.
-------------------------------------------------

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives