Regexp: look ahead and match

Discussion in 'Perl Misc' started by jm, Mar 1, 2004.

  1. jm

    jm Guest

    I was using the following script to find two identical letters in a row,
    and now would like to find two letters in a row that are in alphabetic
    order,
    e.g. ab, or mn, or yz, etc... Haven't had any luck changing the
    script
    to do that. I'm thinking that I should be able to look ahead to the next
    letter.
    Is there a way to increment a backreference to do that?
    Thanks in advance.


    use strict:
    use warnings;

    while (my $str = <> ){
    chomp($str);
    while($string =~ /([a-z])(?=\1)/cgi) {
    print "Match\n";
    }
     
    jm, Mar 1, 2004
    #1
    1. Advertising

  2. jm

    Matt Garrish Guest

    "jm" <> wrote in message
    news:4043c48b$0$3095$...
    > I was using the following script to find two identical letters in a row,
    > and now would like to find two letters in a row that are in alphabetic
    > order,
    > e.g. ab, or mn, or yz, etc... Haven't had any luck changing the
    > script
    > to do that. I'm thinking that I should be able to look ahead to the next
    > letter.
    > Is there a way to increment a backreference to do that?
    > Thanks in advance.
    >
    >
    > use strict:
    > use warnings;
    >
    > while (my $str = <> ){
    > chomp($str);
    > while($string =~ /([a-z])(?=\1)/cgi) {
    > print "Match\n";
    > }
    >


    I don't think it can be done from within a regex (at least I can't think of
    a way that won't result in an eval error). Something simple like the
    following should work, though:

    while (my $str = <>) {

    my $lval = 0;

    foreach my $char ($str =~ /(.)/g) {

    my $ordval = ord($char);

    if ($char =~ /[A-Za-z]/) {
    if ($ordval == ($lval + 1)) {
    print chr($lval) . "$char\n";
    }
    }

    $lval = $ordval;

    }

    }

    Matt
     
    Matt Garrish, Mar 2, 2004
    #2
    1. Advertising

  3. jm

    Jay Tilton Guest

    "jm" <> wrote:

    : I was using the following script to find two identical letters in a row,
    : and now would like to find two letters in a row that are in alphabetic
    : order,
    : e.g. ab, or mn, or yz, etc... Haven't had any luck changing the
    : script
    : to do that. I'm thinking that I should be able to look ahead to the next
    : letter.
    : Is there a way to increment a backreference to do that?

    /([[:alpha:]])(??{ chr(ord($1)+1) })/

    That assumes that alphabetical order and character code order are the same
    thing, which isn't necessarily true.
     
    Jay Tilton, Mar 2, 2004
    #3
  4. jm

    Matt Garrish Guest

    "Jay Tilton" <> wrote in message
    news:...
    > "jm" <> wrote:
    >
    > : I was using the following script to find two identical letters in a row,
    > : and now would like to find two letters in a row that are in alphabetic
    > : order,
    > : e.g. ab, or mn, or yz, etc... Haven't had any luck changing the
    > : script
    > : to do that. I'm thinking that I should be able to look ahead to the

    next
    > : letter.
    > : Is there a way to increment a backreference to do that?
    >
    > /([[:alpha:]])(??{ chr(ord($1)+1) })/
    >


    Brain not function good today. I was using (?{ }) and it just wouldn't work.
    I should've gone back to perlre...

    Is anyone aware of just how "experimental" these extended regexes are? I
    know code can always be rewritten, but I don't like the thought of my
    scripts breaking just because they're being run under a newer version of
    perl (hence I would generally only use something like the above in a
    throw-away script).

    Matt
     
    Matt Garrish, Mar 2, 2004
    #4
  5. jm

    Jay Tilton Guest

    "Matt Garrish" <> wrote:

    : while (my $str = <>) {
    : my $lval = 0;
    : foreach my $char ($str =~ /(.)/g) {
    : my $ordval = ord($char);
    : if ($char =~ /[A-Za-z]/) {
    : if ($ordval == ($lval + 1)) {
    : print chr($lval) . "$char\n";
    : }
    : }
    : $lval = $ordval;
    : }
    : }

    I'm hip on being cautious around any regex patterns labelled as
    "experimental" (re:eek:ther branch of this thread). That technique is
    comparatively bulletproof. The \G regex meta can tighten it up without
    corrupting the spirit of the algorithm.

    while( $str =~ /([[:alpha:]])/g ) {
    my $m = $1;
    my $n = chr( ord($m)+1 );
    print "$m$n\n" if $str =~ /\G$n/;
    }
     
    Jay Tilton, Mar 2, 2004
    #5
  6. jm

    Matt Garrish Guest

    "Jay Tilton" <> wrote in message
    news:...
    > "Matt Garrish" <> wrote:
    >
    > : while (my $str = <>) {
    > : my $lval = 0;
    > : foreach my $char ($str =~ /(.)/g) {
    > : my $ordval = ord($char);
    > : if ($char =~ /[A-Za-z]/) {
    > : if ($ordval == ($lval + 1)) {
    > : print chr($lval) . "$char\n";
    > : }
    > : }
    > : $lval = $ordval;
    > : }
    > : }
    >
    > I'm hip on being cautious around any regex patterns labelled as
    > "experimental" (re:eek:ther branch of this thread). That technique is
    > comparatively bulletproof. The \G regex meta can tighten it up without
    > corrupting the spirit of the algorithm.
    >
    > while( $str =~ /([[:alpha:]])/g ) {
    > my $m = $1;
    > my $n = chr( ord($m)+1 );
    > print "$m$n\n" if $str =~ /\G$n/;
    > }
    >


    I would only comment that you would pay a price in speed by running the
    second regex every time though the loop. Otherwise, as you say, it is much
    more compact.

    Matt
     
    Matt Garrish, Mar 2, 2004
    #6
    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. Prabh
    Replies:
    1
    Views:
    424
    Wendy S
    Sep 11, 2003
  2. Aubrey Hutchison

    GO AHEAD -MAKE ME LOOK DUMB- Please

    Aubrey Hutchison, Dec 31, 2003, in forum: Python
    Replies:
    4
    Views:
    315
    Aubrey Hutchison
    Dec 31, 2003
  3. inhahe
    Replies:
    3
    Views:
    2,369
    Diez B. Roggisch
    Jan 28, 2005
  4. Neil Cerutti

    An iterator with look-ahead

    Neil Cerutti, Jan 10, 2007, in forum: Python
    Replies:
    5
    Views:
    670
    Paddy
    Jan 10, 2007
  5. Replies:
    4
    Views:
    176
Loading...

Share This Page