[thelist] regex help

Chris Platts chris.platts at virelai.co.uk
Wed Sep 3 08:07:31 CDT 2003


On Tuesday, Sep 2, 2003, at 22:37 Europe/London, Tom Dell'Aringa wrote:
>

> I have what is probably a simple regex to do but i cannot figure it
> out. Mainly because I need a variable in my regex. Here is my target
> string:
>
> string = "s1s2s3s4";
>
> from there, I need to remove say "s3".

This worked for me - tested on Mac OS X, Perl 5.6.0, but there must be 
an equivalent in PHP or Python:

--------------------------------------
#! /usr/bin/perl -w

my $students = 's1s233s651s45s65s345s655s7s87';
my $bad = 's65';

$students =~ s/(.*)($bad)(s.*|$)/$1$3/;
--------------------------------------

It correctly removes 's65', leaving 's651' and 's655' intact. 
Dissecting the regular expression:

(.*)	matches zero or more of any character up to the *last* occurence 
of $bad
($bad)	matches the *last* occurence of $bad
(s.* | $)	matches the letter s plus zero or more characters, or the end 
of line

$1$3	replaces with the first and third patterns

If you want to drop the *first* occurence then use (.*?) as the first 
pattern - the ? in this context turns off 'greedy' matching.

Alternatively, avoiding regular expressions and replacing all 
occurences of the identifier:

--------------------------------------
#! /usr/bin/perl -w

my $old = 's1s233s651s45s65s345s655s7s87';
my $bad = '65';
my $new;

foreach my $student ( split( /s/, $old ) ) {
	$new .= 's' . $student if ( $student && $student ne $bad );
}
--------------------------------------

Hope this helps.

Regards,

Chris Platts
Virelai Web Media



More information about the thelist mailing list