Regex: All except leading global substitution...

Discussion in 'Perl Misc' started by Peter Hill, Mar 10, 2006.

  1. Peter Hill

    Peter Hill Guest

    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
     
    Peter Hill, Mar 10, 2006
    #1
    1. Advertising

  2. Peter Hill

    Xicheng Guest

    Peter Hill wrote:
    > 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

    >
    > tia
    > Peter Hill
     
    Xicheng, Mar 10, 2006
    #2
    1. Advertising

  3. Peter Hill

    Uri Guttman Guest

    >>>>> "X" == Xicheng <> writes:

    X> Peter Hill wrote:
    >> 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

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, Mar 10, 2006
    #3
  4. Peter Hill

    Xicheng Guest

    Uri Guttman wrote:
    > >>>>> "X" == Xicheng <> writes:

    >
    > X> Peter Hill wrote:
    > >> 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 :)

    >
    > uri
    >
    > --
    > Uri Guttman ------ -------- http://www.stemsystems.com
    > --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    > Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Xicheng, Mar 10, 2006
    #4
  5. Peter Hill

    Anno Siegel Guest

    Xicheng <> wrote in comp.lang.perl.misc:
    > Peter Hill wrote:
    > > 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
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
     
    Anno Siegel, Mar 10, 2006
    #5
  6. Peter Hill

    Matt Garrish Guest

    "Xicheng" <> wrote in message
    news:...
    > Peter Hill wrote:
    >> 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
     
    Matt Garrish, Mar 10, 2006
    #6
  7. Peter Hill

    Xicheng Guest

    Matt Garrish wrote:
    > "Xicheng" <> wrote in message
    > news:...
    > > Peter Hill wrote:
    > >> 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
     
    Xicheng, Mar 10, 2006
    #7
  8. Peter Hill

    Xicheng Guest

    Anno Siegel wrote:
    > Xicheng <> wrote in comp.lang.perl.misc:


    > > 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

    >
    > > 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
    > --
    > If you want to post a followup via groups.google.com, don't use
    > the broken "Reply" link at the bottom of the article. Click on
    > "show options" at the top of the article, then click on the
    > "Reply" at the bottom of the article headers.
     
    Xicheng, Mar 11, 2006
    #8
  9. Peter Hill

    robic0 Guest

    On Fri, 10 Mar 2006 04:51:08 GMT, "Peter Hill" <> wrote:

    >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-
     
    robic0, Mar 12, 2006
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Salerno
    Replies:
    20
    Views:
    867
    John Salerno
    Aug 11, 2006
  2. John Nagle
    Replies:
    4
    Views:
    604
    James Antill
    Aug 7, 2007
  3. Delaney, Timothy (Tim)
    Replies:
    0
    Views:
    572
    Delaney, Timothy (Tim)
    Aug 7, 2007
  4. Fabio Z Tessitore

    who is simpler? try/except/else or try/except

    Fabio Z Tessitore, Aug 12, 2007, in forum: Python
    Replies:
    5
    Views:
    384
  5. David House

    try -> except -> else -> except?

    David House, Jul 6, 2009, in forum: Python
    Replies:
    2
    Views:
    352
    Bruno Desthuilliers
    Jul 6, 2009
Loading...

Share This Page