Regex: All except leading global substitution...

P

Peter Hill

I think I am misunderstanding how /g works; the following (incorrect)
program does not remove all non-leading vowels, just the 1st non-leading
vowel. Corrections please?

#! /usr/bin/perl -w
use strict;
#remove all non-leading vowels
my $word = 'ABEDU';
$word =~ s/(^..*?)[AEIOU](.*)/$1$2/g;
print qq{$word\n};

tia
Peter Hill
 
X

Xicheng

Peter said:
I think I am misunderstanding how /g works; the following (incorrect)
program does not remove all non-leading vowels, just the 1st non-leading
vowel. Corrections please?

#! /usr/bin/perl -w
use strict;
#remove all non-leading vowels
my $word = 'ABEDU';
$word =~ s/(^..*?)[AEIOU](.*)/$1$2/g;
print qq{$word\n};

you need two archors, one is the beginning of your string, and the
other is \G which records the last matched position, so you may try
things like:

$word =~ s/(^.|\G)(.*?)([aeiou])/\1\2/gi;

also, you need to use \1 \2 instead of $1 $2 if you don't use /e
modifier..

Xicheng
 
U

Uri Guttman

X> Peter Hill said:
I think I am misunderstanding how /g works; the following (incorrect)
program does not remove all non-leading vowels, just the 1st non-leading
vowel. Corrections please?

#! /usr/bin/perl -w
use strict;
#remove all non-leading vowels
my $word = 'ABEDU';
$word =~ s/(^..*?)[AEIOU](.*)/$1$2/g;
print qq{$word\n};

X> you need two archors, one is the beginning of your string, and the
X> other is \G which records the last matched position, so you may try
X> things like:

X> $word =~ s/(^.|\G)(.*?)([aeiou])/\1\2/gi;

X> also, you need to use \1 \2 instead of $1 $2 if you don't use /e
X> modifier..

you are all working too hard. this seems to work fine (according to what
i understand of the specs):

perl -pe 's/([^aeiou]+)[aeiou]+/$1/ig'
andhekjajd
andhkjjd
weh1ajajkrkew
wh1jjkrkw
aaammmmddeww
aaammmmddww

uri
 
X

Xicheng

Uri said:
X> Peter Hill said:
I think I am misunderstanding how /g works; the following (incorrect)
program does not remove all non-leading vowels, just the 1st non-leading
vowel. Corrections please?

#! /usr/bin/perl -w
use strict;
#remove all non-leading vowels
my $word = 'ABEDU';
$word =~ s/(^..*?)[AEIOU](.*)/$1$2/g;
print qq{$word\n};

X> you need two archors, one is the beginning of your string, and the
X> other is \G which records the last matched position, so you may try
X> things like:

X> $word =~ s/(^.|\G)(.*?)([aeiou])/\1\2/gi;

X> also, you need to use \1 \2 instead of $1 $2 if you don't use /e
X> modifier..

you are all working too hard. this seems to work fine (according to what
i understand of the specs):

perl -pe 's/([^aeiou]+)[aeiou]+/$1/ig'
andhekjajd
andhkjjd
weh1ajajkrkew
wh1jjkrkw
aaammmmddeww
aaammmmddww

Good point, but we don't need to stick to one way, one tool, one
thought to do anything in Perl or in whatever, hehe.. :)

Good day,
Xicheng :)
 
A

Anno Siegel

Xicheng said:
Peter said:
I think I am misunderstanding how /g works; the following (incorrect)
program does not remove all non-leading vowels, just the 1st non-leading
vowel. Corrections please?

#! /usr/bin/perl -w
use strict;
#remove all non-leading vowels
my $word = 'ABEDU';
$word =~ s/(^..*?)[AEIOU](.*)/$1$2/g;
print qq{$word\n};

you need two archors, one is the beginning of your string, and the
other is \G which records the last matched position, so you may try
things like:

$word =~ s/(^.|\G)(.*?)([aeiou])/\1\2/gi;

That only keeps the initial vowel if there is another vowel in the string.
"andhkjjd" is changed to "ndhkjjd". Also, as I understand the OP, any
initial *group* of vowels should be kept, not only the first one.
also, you need to use \1 \2 instead of $1 $2 if you don't use /e
modifier..

This is plain wrong. \1, \2, etc. should be used if you need to refer
to a capture in the regex itself. On the substitution side of s///,
$1, $2 etc. are correct. Run it under warnings and see.

Anno
 
M

Matt Garrish

Xicheng said:
Peter said:
I think I am misunderstanding how /g works; the following (incorrect)
program does not remove all non-leading vowels, just the 1st non-leading
vowel. Corrections please?

#! /usr/bin/perl -w
use strict;
#remove all non-leading vowels
my $word = 'ABEDU';
$word =~ s/(^..*?)[AEIOU](.*)/$1$2/g;
print qq{$word\n};


$word =~ s/(^.|\G)(.*?)([aeiou])/\1\2/gi;

also, you need to use \1 \2 instead of $1 $2 if you don't use /e
modifier..

Huh? Care to explain where you got that idea from? You use backreferences
inside the match and numbered variables on the substitution side. You *can*
do what you've done above, but see perlre for the pitfalls.

Matt
 
X

Xicheng

Matt said:
Xicheng said:
Peter said:
I think I am misunderstanding how /g works; the following (incorrect)
program does not remove all non-leading vowels, just the 1st non-leading
vowel. Corrections please?

#! /usr/bin/perl -w
use strict;
#remove all non-leading vowels
my $word = 'ABEDU';
$word =~ s/(^..*?)[AEIOU](.*)/$1$2/g;
print qq{$word\n};


$word =~ s/(^.|\G)(.*?)([aeiou])/\1\2/gi;

also, you need to use \1 \2 instead of $1 $2 if you don't use /e
modifier..

Huh? Care to explain where you got that idea from? You use backreferences
inside the match and numbered variables on the substitution side. You *can*
do what you've done above, but see perlre for the pitfalls.

Hi, Thanks Anno and Matt for correcting me out. these days, I am
busying in learning some other new stuff, so may mix up something
here.. But anyhow in the last several months, I've learnt a lot from
this group either from other ppl's posts or from my own mistakes. As
for me, it's not that bad to refresh myself everyday, and upon my
graduation, I hope I will be gathering more knowledge in Perl and feel
more confidence about it.. :)

Have a good weekend,
Xicheng
 
X

Xicheng

Anno said:
you need two archors, one is the beginning of your string, and the
other is \G which records the last matched position, so you may try
things like:

$word =~ s/(^.|\G)(.*?)([aeiou])/\1\2/gi;

That only keeps the initial vowel if there is another vowel in the string.
"andhkjjd" is changed to "ndhkjjd". Also, as I understand the OP, any
initial *group* of vowels should be kept, not only the first one.

There is a bug in my regex as you said, but it might be fixed by
separating ^. part with the other part, the way to solve this problem
should not be wrong...,say:
change:
$word =~ s/(^.|\G)(.*?)([aeiou])/\1\2/gi;
to:
$word =~ s/(^.)|\G(.*?)[aeiou]/$1$2/gi;

this deletes the degenerate case when there is no match of
"^.(.*?)[aeiou]" and "\G" becomes the beginning of the string..

Xicheng
 
R

robic0

I think I am misunderstanding how /g works; the following (incorrect)
program does not remove all non-leading vowels, just the 1st non-leading
vowel. Corrections please?

#! /usr/bin/perl -w
use strict;
#remove all non-leading vowels
my $word = 'ABEDU';
$word =~ s/(^..*?)[AEIOU](.*)/$1$2/g;
print qq{$word\n};

tia
Peter Hill
I might be missing some hidden detail in yur explaination.
It might be a convolution of the sample 'ABEDU' with the
problem statement "does not remove all non-leading vowels,
just the 1st non-leading vowel."

This removes *ALL* of the character class defined.
You can't say all non leading vowels without defining
what "leading" is, and you certaintly don't do that here.

use strict;
#remove all non-leading vowels
my $word = 'ABEDU';
$word =~ s/[AEIOU]//g;
print qq{$word\n};

Back to class pigmy!!
-robic0-
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top