Regular expression don't match sequence of characters

Discussion in 'Perl Misc' started by Big Tony, Sep 27, 2004.

  1. Big Tony

    Big Tony Guest

    Hi

    I suspect this is an easy one and sits somewhere in a FAQ, but can
    anyone help anyway?

    Suppose I have two strings:
    $str1 = "abcd%efg";
    $str2 = "1234\%567";

    I would a regular expression that will return "abcd" from $str1 and
    "1234\%567" from $str2, i.e. if a "%" is encountered without being
    preceded by a "\" then ignore it and the remainder of the string, but
    if "\%" is encountered do nothing special and return it with the rest
    of the string.
    I guess I could use split to break the strings around the "%" and then
    test for a trailing "\", but wondered if anyone could come up with a
    one-liner.

    cheers

    BT
     
    Big Tony, Sep 27, 2004
    #1
    1. Advertising

  2. Big Tony <> wrote:

    > Suppose I have two strings:
    > $str1 = "abcd%efg";
    > $str2 = "1234\%567";



    I guess you haven't tried print()ing the value of $str2 at this point, huh?


    > I would a regular expression that will return "abcd" from $str1 and
    > "1234\%567" from $str2, i.e. if a "%" is encountered without being
    > preceded by a "\" then ignore it and the remainder of the string, but
    > if "\%" is encountered do nothing special and return it with the rest
    > of the string.



    None of your strings have a percent preceded by a backslash...


    Anyway, you can use a "negative look-behind":

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

    foreach ( 'abcd%efg', '1234\%567' ) {
    print "string is '$_'\n";
    # my( $stuff ) = /^(.*?)((?<!\\)%|$)/;
    my( $stuff ) = /^(.*?) # bunch of chars until...
    (
    (?<!\\)% # ... a percent without preceding backslash
    | # or
    $ # end of string
    )/sx;
    print "stuff is '$stuff'\n";
    }
    -------------------------------------


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Sep 27, 2004
    #2
    1. Advertising

  3. Big Tony

    Anno Siegel Guest

    Big Tony <> wrote in comp.lang.perl.misc:
    > Hi
    >
    > I suspect this is an easy one and sits somewhere in a FAQ, but can
    > anyone help anyway?
    >
    > Suppose I have two strings:
    > $str1 = "abcd%efg";
    > $str2 = "1234\%567";


    Note that $str2 does *not* contain a backslash. Either use single
    quotes, or double the \:

    $str2 = '1234\%567';
    or
    $str2 = "1234\\%567"

    > I would a regular expression that will return "abcd" from $str1 and
    > "1234\%567" from $str2, i.e. if a "%" is encountered without being
    > preceded by a "\" then ignore it and the remainder of the string, but
    > if "\%" is encountered do nothing special and return it with the rest
    > of the string.
    > I guess I could use split to break the strings around the "%" and then
    > test for a trailing "\", but wondered if anyone could come up with a
    > one-liner.


    Let's first find a regex that matches a single "%", but not a "\%".
    You want negative lookbehind for that:

    /(?<!\\)%/

    (Again, \ must be doubled in double-quotish context.)

    To delete that match, plus everything that follows, do

    s/(?<!\\)%.*//;

    Anno
     
    Anno Siegel, Sep 27, 2004
    #3
    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. championsleeper
    Replies:
    6
    Views:
    1,040
    championsleeper
    Apr 6, 2004
  2. Liang
    Replies:
    2
    Views:
    1,738
  3. VSK
    Replies:
    2
    Views:
    2,353
  4. Roger L. Cauvin

    Match First Sequence in Regular Expression?

    Roger L. Cauvin, Jan 26, 2006, in forum: Python
    Replies:
    43
    Views:
    1,165
    Armin Steinhoff
    Jan 28, 2006
  5. Replies:
    11
    Views:
    286
    Raul Parolari
    Dec 2, 2007
Loading...

Share This Page