Leading Space with REGEX

Discussion in 'Perl Misc' started by jerryacoleman@gmail.com, Mar 29, 2007.

  1. Guest

    I have the following regex. It is matching what I want( /BIG/TRUCK),
    but it is also included the leading space. Is there anyway to
    suppress the leading space.

    \b[^"|']/\w+/\w+\b

    String:

    TYPE /BIG/TRUCK
    , Mar 29, 2007
    #1
    1. Advertising

  2. Paul Lalli Guest

    On Mar 29, 12:20 pm, wrote:
    > I have the following regex. It is matching what I want( /BIG/TRUCK),
    > but it is also included the leading space. Is there anyway to
    > suppress the leading space.
    >
    > \b[^"|']/\w+/\w+\b
    >
    > String:
    >
    > TYPE /BIG/TRUCK


    I don't understand your question. You're very specifically looking
    for "word boundary; anything other than double quote, vertical-bar, or
    single quote; slash; one or more word characters; slash; one or more
    word characters; word boundary". The space character matches the
    "anything other than..." portion of that. If you don't want to match
    that space character, why are you putting that token in there?

    To put it another way: Please post a SHORT but COMPLETE script that
    demonstrates both what you're doing, and the output that demonstrates
    what you want to do.

    Paul Lalli
    Paul Lalli, Mar 29, 2007
    #2
    1. Advertising

  3. Guest

    "Paul Lalli" <> writes:

    > On Mar 29, 12:20 pm, wrote:
    > > I have the following regex. It is matching what I want( /BIG/TRUCK),
    > > but it is also included the leading space. Is there anyway to
    > > suppress the leading space.
    > >
    > > \b[^"|']/\w+/\w+\b
    > >
    > > String:
    > >
    > > TYPE /BIG/TRUCK

    >
    > I don't understand your question. You're very specifically looking
    > for "word boundary; anything other than double quote, vertical-bar, or
    > single quote; slash; one or more word characters; slash; one or more
    > word characters; word boundary". The space character matches the
    > "anything other than..." portion of that. If you don't want to match
    > that space character, why are you putting that token in there?
    >
    > To put it another way: Please post a SHORT but COMPLETE script that
    > demonstrates both what you're doing, and the output that demonstrates
    > what you want to do.
    >
    > Paul Lalli


    Jerry, and all other people with Regex type questions;
    If you read the posting guidelines that are posted frequently
    and easily googleable for this news group, you'll see that what
    Paul means by "SHOFT but COMPLETE program"
    is something like this:

    ==cut==
    use warnings; # see perldoc warnings
    use strict; # see perldoc strict
    while (<DATA>){ # get a line from below __DATA__ in to $_ default var
    chomp;
    my ($saved) = ( m:(\b[^"|']/\w+/\w+\b): );
    print "Line $. is '$_', saved section is '$saved'\n";
    }
    __DATA__
    TYPE /BIG/TRUCK
    ECHO /SMALL/CAR
    ==cut==

    # Here is the output I have:
    Line 1 is 'TYPE /BIG/TRUCK', saved section is ' /BIG/TRUCK'
    Line 2 is 'ECHO /SMALL/CAR', saved section is ' /SMALL/CAR'

    # Here is the output I desire:
    Line 1 is 'TYPE /BIG/TRUCK', saved section is '/BIG/TRUCK'
    Line 2 is 'ECHO /SMALL/CAR', saved section is '/SMALL/CAR'

    ######
    With the above type of data, it is easy for anyone to
    copy and paste your complete program with DATA and run it
    to demonstrate. Plus, it helps you find the problem yourself.

    Hey look, if I follow Paul's adice and move the ( to after
    the [] section, it generates the desired output.
    was my ($saved) = ( m:(\b[^"|']/\w+/\w+\b): );
    now my ($saved) = ( m:\b[^"|'](/\w+/\w+\b): );
    ^------>(

    --
    Joel
    , Mar 29, 2007
    #3
  4. wrote:
    > I have the following regex. It is matching what I want( /BIG/TRUCK),
    > but it is also included the leading space. Is there anyway to
    > suppress the leading space.
    >
    > \b[^"|']/\w+/\w+\b
    >
    > String:
    >
    > TYPE /BIG/TRUCK


    \b matches at six places in the above example, between the beginning of the
    string and the 'T' in TYPE, between the 'E' in TYPE the ' ' following it,
    between the '/' and the 'B' in BIG, between the 'G' in BIG and the '/',
    between the '/' and the 'T' in TRUCK, and finally, between the 'K' in TRUCK
    and the end of the string.

    In your example the only place the the first \b can match is between the 'E'
    in TYPE the ' ' following it and the second \b is superfluous because \w+ is
    greedy so anything following it will not match \w.



    John
    --
    Perl isn't a toolbox, but a small machine shop where you can special-order
    certain sorts of tools at low cost and in short order. -- Larry Wall
    John W. Krahn, Mar 29, 2007
    #4
    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. Shuo Xiang

    Stack space, global space, heap space

    Shuo Xiang, Jul 9, 2003, in forum: C Programming
    Replies:
    10
    Views:
    2,846
    Bryan Bullard
    Jul 11, 2003
  2. Christian Seberino
    Replies:
    21
    Views:
    1,605
    Stephen Horne
    Oct 27, 2003
  3. Ian Bicking
    Replies:
    2
    Views:
    967
    Steve Lamb
    Oct 23, 2003
  4. Ian Bicking
    Replies:
    2
    Views:
    696
    Michael Hudson
    Oct 24, 2003
  5. John Nagle
    Replies:
    4
    Views:
    574
    James Antill
    Aug 7, 2007
Loading...

Share This Page