Regex Question

Discussion in 'Perl Misc' started by Mike C#, Apr 4, 2006.

  1. Mike C#

    Mike C# Guest

    Hi I'm new to Perl regex's, and I've been racking my brain for a while now
    on this one. Any help is appreciated. Basically, I want to return all
    matches from the beginning of the string to a specific letter. As an
    example, with the string "Manager" and up to the letter "a", I want to
    return the following sub-groups:

    M
    Man

    And for the word "Fishing" and up to the letter "i", I want to return:

    F
    Fish

    Thank you in advance.

    Mike C.
    Mike C#, Apr 4, 2006
    #1
    1. Advertising

  2. Mike C#

    Keith Keller Guest

    On 2006-04-04, Mike C# <> wrote:
    > Hi I'm new to Perl regex's, and I've been racking my brain for a while now
    > on this one. Any help is appreciated. Basically, I want to return all
    > matches from the beginning of the string to a specific letter. As an
    > example, with the string "Manager" and up to the letter "a", I want to
    > return the following sub-groups:
    >
    > M
    > Man
    >
    > And for the word "Fishing" and up to the letter "i", I want to return:
    >
    > F
    > Fish



    One question, one comment.

    Question: What do you do with "Manamager"?

    Comment: What did you try? Please post some short code examples.

    --keith

    --
    -francisco.ca.us
    (try just my userid to email me)
    AOLSFAQ=http://wombat.san-francisco.ca.us/cgi-bin/fom
    see X- headers for PGP signature information
    Keith Keller, Apr 4, 2006
    #2
    1. Advertising

  3. Mike C# wrote:
    > Hi I'm new to Perl regex's, and I've been racking my brain for a while now
    > on this one. Any help is appreciated. Basically, I want to return all
    > matches from the beginning of the string to a specific letter. As an
    > example, with the string "Manager" and up to the letter "a", I want to
    > return the following sub-groups:
    >
    > M
    > Man
    >
    > And for the word "Fishing" and up to the letter "i", I want to return:
    >
    > F
    > Fish


    You don't need a regex for that:

    $ perl -le'
    $str = q[Manager];
    $letter = q[a];
    $len = 0;
    print substr $str, 0, $len while ( $len = index $str, $letter, $len + 1 ) > 0;
    '
    M
    Man

    $ perl -le'
    $str = q[Fishing];
    $letter = q;
    $len = 0;
    print substr $str, 0, $len while ( $len = index $str, $letter, $len + 1 ) > 0;
    '
    F
    Fish



    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn, Apr 4, 2006
    #3
  4. Mike C#

    Xicheng Guest

    Mike C# wrote:
    > Hi I'm new to Perl regex's, and I've been racking my brain for a while now
    > on this one. Any help is appreciated. Basically, I want to return all
    > matches from the beginning of the string to a specific letter. As an
    > example, with the string "Manager" and up to the letter "a", I want to
    > return the following sub-groups:
    >
    > M
    > Man
    >
    > And for the word "Fishing" and up to the letter "i", I want to return:
    >
    > F
    > Fish
    >
    > Thank you in advance.


    If you want to match till the right-most instance of a specific
    charater, you may use substr() and rindex(), i.e.:

    my $str = substr $string, 0, rindex( $string, $letter );

    or the following regex

    my ($str) = $string =~ /^(.*)(?=$letter)/;

    =========
    #!/usr/bin/perl
    use warnings;
    use strict;

    my %strings = ( Fishing => "i",
    Banana => "a",
    Manage => "a",
    );

    print join "\n",
    map { substr $_, 0, rindex( $_, $strings{ $_ } ) }
    # map { /^(.*)(?=$strings{$_})/ } # regex
    keys %strings;
    ==============printout=============
    Man
    Fish
    Banan
    ==========

    Xicheng

    > Mike C.
    Xicheng, Apr 4, 2006
    #4
  5. Mike C#

    Dr.Ruud Guest

    letter-stepper (was: Re: Regex Question)

    Xicheng schreef:
    > Mike C#:


    >> Hi I'm new to Perl regex's, and I've been racking my brain for a
    >> while now on this one. Any help is appreciated. Basically, I want
    >> to return all matches from the beginning of the string to a specific
    >> letter. [...]

    >
    > If you want to match till the right-most instance of a specific
    > charater, you may use substr() and rindex()


    Read again, he wants 'Manager' to produce both 'M' and 'Man'.

    (Banana, a) -> (B, Ban, Banan)
    (Mississippi, s) -> (Mi, Mis, Missi, Missis)
    (Mississippi, i) -> (M, Miss, Mississ, Mississipp)

    So subst + index, like John W. showed.

    But what if the string starts with the special letter?

    (abracadabra, a) -> (<nil>, abr, abrac, abracad, abracadabr)
    (abracadabra, a) -> (abr, abrac, abracad, abracadabr)

    --
    Affijn, Ruud

    "Gewoon is een tijger."
    Dr.Ruud, Apr 4, 2006
    #5
  6. Mike C#

    Anno Siegel Guest

    John W. Krahn <> wrote in comp.lang.perl.misc:
    > Mike C# wrote:
    > > Hi I'm new to Perl regex's, and I've been racking my brain for a while now
    > > on this one. Any help is appreciated. Basically, I want to return all
    > > matches from the beginning of the string to a specific letter. As an
    > > example, with the string "Manager" and up to the letter "a", I want to
    > > return the following sub-groups:
    > >
    > > M
    > > Man
    > >
    > > And for the word "Fishing" and up to the letter "i", I want to return:
    > >
    > > F
    > > Fish

    >
    > You don't need a regex for that:
    >
    > $ perl -le'
    > $str = q[Manager];
    > $letter = q[a];
    > $len = 0;
    > print substr $str, 0, $len while ( $len = index $str, $letter, $len + 1 ) > 0;
    > '
    > M
    > Man


    Then again, a regex can be used:

    print substr($str, 0, $-[0]), "\n" while $str =~ /$letter/g;

    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, Apr 4, 2006
    #6
  7. Mike C# wrote:
    > Hi I'm new to Perl regex's, and I've been racking my brain for a
    > while now on this one. Any help is appreciated. Basically, I want
    > to return all matches from the beginning of the string to a specific
    > letter. As an example, with the string "Manager" and up to the
    > letter "a", I want to return the following sub-groups:
    >
    > M
    > Man
    >
    > And for the word "Fishing" and up to the letter "i", I want to return:
    >
    > F
    > Fish
    >
    > Thank you in advance.


    Totally different idea:

    reverse() the string
    while() the string still contains the letter
    chop off the beginning of the string up to and including the first
    occurence of that letter
    the reverse() of the current string is another solution

    jue
    Jürgen Exner, Apr 4, 2006
    #7
  8. Mike C#

    Anno Siegel Guest

    Jürgen Exner <> wrote in comp.lang.perl.misc:
    > Mike C# wrote:
    > > Hi I'm new to Perl regex's, and I've been racking my brain for a
    > > while now on this one. Any help is appreciated. Basically, I want
    > > to return all matches from the beginning of the string to a specific
    > > letter. As an example, with the string "Manager" and up to the
    > > letter "a", I want to return the following sub-groups:
    > >
    > > M
    > > Man
    > >
    > > And for the word "Fishing" and up to the letter "i", I want to return:
    > >
    > > F
    > > Fish
    > >
    > > Thank you in advance.

    >
    > Totally different idea:
    >
    > reverse() the string
    > while() the string still contains the letter
    > chop off the beginning of the string up to and including the first
    > occurence of that letter
    > the reverse() of the current string is another solution


    Why all the reversal? Chop off the letter and everything that follows
    it on each step:

    print "$str\n" while $str =~ s/$letter[^$letter]*$//;

    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, Apr 4, 2006
    #8
  9. Mike C#

    Dr.Ruud Guest

    Anno Siegel schreef:

    > print "$str\n" while $str =~ s/$letter[^$letter]*$//;


    echo Bananana
    | perl -nle 'print "<$1>" while /^(.*?\G.*?)a/g'

    --
    Affijn, Ruud

    "Gewoon is een tijger."
    Dr.Ruud, Apr 4, 2006
    #9
  10. Anno Siegel wrote:
    >
    > John W. Krahn <> wrote in comp.lang.perl.misc:
    >>
    >>You don't need a regex for that:
    >>
    >>$ perl -le'
    >>$str = q[Manager];
    >>$letter = q[a];
    >>$len = 0;
    >>print substr $str, 0, $len while ( $len = index $str, $letter, $len + 1 ) > 0;
    >>'
    >>M
    >>Man

    >
    > Then again, a regex can be used:
    >
    > print substr($str, 0, $-[0]), "\n" while $str =~ /$letter/g;


    The difference is that that prints the string '' if substr( $str, 0, 1 ) eq
    $letter. If you want the same behavior with substr/index change it to:

    $len = -1;
    print substr $str, 0, $len while ( $len = index $str, $letter, $len + 1 ) >= 0;




    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn, Apr 4, 2006
    #10
  11. Mike C#

    Xicheng Guest

    Re: letter-stepper (was: Re: Regex Question)

    Dr.Ruud wrote:
    > Xicheng schreef:
    > > Mike C#:

    >
    > >> Hi I'm new to Perl regex's, and I've been racking my brain for a
    > >> while now on this one. Any help is appreciated. Basically, I want
    > >> to return all matches from the beginning of the string to a specific
    > >> letter. [...]

    > >
    > > If you want to match till the right-most instance of a specific
    > > charater, you may use substr() and rindex()

    >
    > Read again, he wants 'Manager' to produce both 'M' and 'Man'.
    >
    > (Banana, a) -> (B, Ban, Banan)
    > (Mississippi, s) -> (Mi, Mis, Missi, Missis)
    > (Mississippi, i) -> (M, Miss, Mississ, Mississipp)
    >
    > So subst + index, like John W. showed.
    >
    > But what if the string starts with the special letter?
    >
    > (abracadabra, a) -> (<nil>, abr, abrac, abracad, abracadabr)
    > (abracadabra, a) -> (abr, abrac, abracad, abracadabr)


    ou, I did not read it carefully, but it might still be done some other
    ways:

    print $str .= $1 while $string =~ /\G(.+?)(?=$letter)/g;

    Xicheng Jia :)
    ====

    > --
    > Affijn, Ruud
    >
    > "Gewoon is een tijger."
    Xicheng, Apr 4, 2006
    #11
  12. Mike C#

    Xicheng Guest

    Re: letter-stepper (was: Re: Regex Question)

    Dr.Ruud wrote:
    > Xicheng schreef:
    > > Mike C#:

    >
    > >> Hi I'm new to Perl regex's, and I've been racking my brain for a
    > >> while now on this one. Any help is appreciated. Basically, I want
    > >> to return all matches from the beginning of the string to a specific
    > >> letter. [...]

    > >
    > > If you want to match till the right-most instance of a specific
    > > charater, you may use substr() and rindex()

    >
    > Read again, he wants 'Manager' to produce both 'M' and 'Man'.
    >
    > (Banana, a) -> (B, Ban, Banan)
    > (Mississippi, s) -> (Mi, Mis, Missi, Missis)
    > (Mississippi, i) -> (M, Miss, Mississ, Mississipp)
    >
    > So subst + index, like John W. showed.
    >
    > But what if the string starts with the special letter?
    >
    > (abracadabra, a) -> (<nil>, abr, abrac, abracad, abracadabr)
    > (abracadabra, a) -> (abr, abrac, abracad, abracadabr)


    ou, I did not read it carefully, but it might still be done some other
    ways:

    print $str .= $1 while $string =~ /\G(.+?)(?=$letter)/g;

    Xicheng Jia :)
    ====

    > --
    > Affijn, Ruud
    >
    > "Gewoon is een tijger."
    Xicheng, Apr 4, 2006
    #12
  13. Mike C#

    Xicheng Guest

    Re: letter-stepper (was: Re: Regex Question)

    Dr.Ruud wrote:
    > Xicheng schreef:
    > > Mike C#:

    >
    > >> Hi I'm new to Perl regex's, and I've been racking my brain for a
    > >> while now on this one. Any help is appreciated. Basically, I want
    > >> to return all matches from the beginning of the string to a specific
    > >> letter. [...]

    > >
    > > If you want to match till the right-most instance of a specific
    > > charater, you may use substr() and rindex()

    >
    > Read again, he wants 'Manager' to produce both 'M' and 'Man'.
    >
    > (Banana, a) -> (B, Ban, Banan)
    > (Mississippi, s) -> (Mi, Mis, Missi, Missis)
    > (Mississippi, i) -> (M, Miss, Mississ, Mississipp)
    >
    > So subst + index, like John W. showed.


    dr => But what if the string starts with the special letter?

    dr => (abracadabra, a) -> (<nil>, abr, abrac, abracad, abracadabr)
    dr => (abracadabra, a) -> (abr, abrac, abracad, abracadabr)

    For these kinds of cases:

    perl -le '$_="iiMississippi"; print $x.=$1 while /(^|.+?)(?=i)/g'

    ======

    i
    iiM
    iiMiss
    iiMississ
    iiMississipp
    =============

    Xicheng

    > --
    > Affijn, Ruud
    >
    > "Gewoon is een tijger."
    Xicheng, Apr 4, 2006
    #13
  14. Mike C#

    Dr.Ruud Guest

    Re: letter-stepper

    Xicheng schreef:


    > [ (aBanana, a) -> (aB, aBan, aBanan) ]
    >
    > print $str .= $1 while $string =~ /\G(.+?)(?=$letter)/g;


    Yes, nice indeed. The \G is not necessary, as you showed with

    print $str .= $1 while $string =~ /(^|.+?)(?=$letter)/g'

    (slightly reformatted)

    --
    Affijn, Ruud

    "Gewoon is een tijger."
    Dr.Ruud, Apr 4, 2006
    #14
  15. Mike C#

    Guest

    Anno Siegel <-berlin.de> wrote:
    > John W. Krahn <> wrote in comp.lang.perl.misc:
    >> Mike C# wrote:
    >> > Hi I'm new to Perl regex's, and I've been racking my brain for a while now
    >> > on this one. Any help is appreciated. Basically, I want to return all
    >> > matches from the beginning of the string to a specific letter. As an
    >> > example, with the string "Manager" and up to the letter "a", I want to
    >> > return the following sub-groups:


    >> > M
    >> > Man


    >> > And for the word "Fishing" and up to the letter "i", I want to return:


    >> > F
    >> > Fish


    > Then again, a regex can be used:


    > print substr($str, 0, $-[0]), "\n" while $str =~ /$letter/g;


    or...

    print $`, "\n" while $str =~ /$letter/g;

    Axel
    , Apr 4, 2006
    #15
  16. Re: letter-stepper

    Dr.Ruud wrote:
    > Xicheng schreef:
    >
    >
    > > [ (aBanana, a) -> (aB, aBan, aBanan) ]
    > >
    > > print $str .= $1 while $string =~ /\G(.+?)(?=$letter)/g;

    >
    > Yes, nice indeed. The \G is not necessary, as you showed with
    >
    > print $str .= $1 while $string =~ /(^|.+?)(?=$letter)/g'


    should the above be:

    s/ing//;
    it_says_BALLS_on_your forehead, Apr 4, 2006
    #16
  17. Mike C#

    Dr.Ruud Guest

    Re: letter-stepper

    it_says_BALLS_on_your forehead schreef:
    > Dr.Ruud:
    >> Xicheng:


    >>> [ (aBanana, a) -> (aB, aBan, aBanan) ]
    >>>
    >>> print $str .= $1 while $string =~ /\G(.+?)(?=$letter)/g;

    >>
    >> Yes, nice indeed. The \G is not necessary, as you showed with
    >>
    >> print $str .= $1 while $string =~ /(^|.+?)(?=$letter)/g'

    >
    > should the above be:
    >
    > s/ing//;


    No, it was implied that $string contains something like 'Mississippi'
    and $letter for example 'i'.
    The $str collects the captured strings.

    echo "a banana"
    | perl -nle 'print "<", $str .= $1, ">" while /(^|.+?)(?=a)/g'

    --
    Affijn, Ruud

    "Gewoon is een tijger."
    Dr.Ruud, Apr 4, 2006
    #17
  18. Mike C#

    Dr.Ruud Guest

    schreef:
    > Anno Siegel:
    >>> Mike C#:


    >>>> I
    >>>> want to return all matches from the beginning of the string to a
    >>>> specific letter. As an example, with the string "Manager" and up
    >>>> to the letter "a", I want to return the following sub-groups:
    >>>> M
    >>>> Man
    >>>>
    >>>> And for the word "Fishing" and up to the letter "i", I want to
    >>>> return:
    >>>> F
    >>>> Fish

    >>
    >> print substr($str, 0, $-[0]), "\n" while $str =~ /$letter/g;

    >
    > print $`, "\n" while $str =~ /$letter/g;


    Of course, and to me the nicest so far.

    Test:
    echo abracadabra | perl -nle 'print "<$`>" while /a/g'

    --
    Affijn, Ruud

    "Gewoon is een tijger."
    Dr.Ruud, Apr 4, 2006
    #18
  19. Mike C#

    Mike C# Guest

    Thank you all for your help on this. Your regular expression examples
    worked great. I'm pretty familiar with substr functions, looping
    structures, iterators, arrays, etc., etc. This was all about the regex's.

    To answer the question why would I want to do this? More out of curiousity
    than anything. As I mentioned, I'm learning regex's and I'm finding out
    it's more art than science. I was focusing on the ?: and ?= operators but
    getting nowhere fast. Basically I was looking for a more "elegant" way
    other than looping.

    Thanks!

    "Mike C#" <> wrote in message news:yllYf.548$...
    > Hi I'm new to Perl regex's, and I've been racking my brain for a while now
    > on this one. Any help is appreciated. Basically, I want to return all
    > matches from the beginning of the string to a specific letter. As an
    > example, with the string "Manager" and up to the letter "a", I want to
    > return the following sub-groups:
    >
    > M
    > Man
    >
    > And for the word "Fishing" and up to the letter "i", I want to return:
    >
    > F
    > Fish
    >
    > Thank you in advance.
    >
    > Mike C.
    >
    Mike C#, Apr 4, 2006
    #19
    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. =?Utf-8?B?SmViQnVzaGVsbA==?=

    Is ASP Validator Regex Engine Same As VS2003 Find Regex Engine?

    =?Utf-8?B?SmViQnVzaGVsbA==?=, Oct 22, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    698
    =?Utf-8?B?SmViQnVzaGVsbA==?=
    Oct 22, 2005
  2. Rick Venter

    perl regex to java regex

    Rick Venter, Oct 29, 2003, in forum: Java
    Replies:
    5
    Views:
    1,622
    Ant...
    Nov 6, 2003
  3. Replies:
    2
    Views:
    596
  4. Xah Lee
    Replies:
    1
    Views:
    939
    Ilias Lazaridis
    Sep 22, 2006
  5. Replies:
    3
    Views:
    754
    Reedick, Andrew
    Jul 1, 2008
Loading...

Share This Page