Q: Regexp for fixed-length string with trailing whitespace

Discussion in 'Perl Misc' started by andreas1234567@hotmail.com, Sep 9, 2005.

  1. Guest

    Hi,

    I want to create a regexp in order to match N word characters followed
    by M trailing space(s), where N+M=<a fixed number>, N>1, M>=0.

    Not-quite-what-I-want-to-accomplish sample:

    use strict;
    use warnings;
    my @data = (
    "39 ", # ok
    "0000 ", # ok
    "0139 ", # ok
    "000139", # ok
    " 0139", # nok
    );
    for (@data) {
    # wanted behavior is to match on 6 word characters, or any word
    # characters with trailing space(s), 6 characters long in total.
    print "'$_': "; (m/^(\w+\s*){6}$/) ? print "ok\n" : print "nok\n";
    }
    __END__
    '39 ': nok
    '0000 ': nok
    '0139 ': nok
    '000139': ok
    ' 0139': nok

    Any regexp masters online?

    --
    Andreas
     
    , Sep 9, 2005
    #1
    1. Advertising

  2. wrote in news:1126266525.428554.270650
    @g43g2000cwa.googlegroups.com:

    > I want to create a regexp in order to match N word characters followed
    > by M trailing space(s), where N+M=<a fixed number>, N>1, M>=0.
    >

    use strict;
    use warnings;

    my @data = (
    "39 ", # ok
    "0000 ", # ok
    "0139 ", # ok
    "000139", # ok
    " 0139", # nok
    );

    for (@data) {
    chomp;
    print "$_: ";
    (m/^\w{1,6}\s{0,5}$/) ? print "ok\n" : print "nok\n";
    }

    > --
    > Andreas


    Your sig separator is wrong. It should be dash-dash-space-newline. As it
    is, the space character is missing.

    Sinan

    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Sep 9, 2005
    #2
    1. Advertising

  3. Anno Siegel Guest

    <> wrote in comp.lang.perl.misc:
    > Hi,
    >
    > I want to create a regexp in order to match N word characters followed
    > by M trailing space(s), where N+M=<a fixed number>, N>1, M>=0.


    I'm reading the condition as "N >= 1, M >= 0". If you actually mean
    "N > 1" the modifications are simple.

    > Not-quite-what-I-want-to-accomplish sample:
    >
    > use strict;
    > use warnings;
    > my @data = (
    > "39 ", # ok
    > "0000 ", # ok
    > "0139 ", # ok
    > "000139", # ok
    > " 0139", # nok
    > );
    > for (@data) {
    > # wanted behavior is to match on 6 word characters, or any word
    > # characters with trailing space(s), 6 characters long in total.
    > print "'$_': "; (m/^(\w+\s*){6}$/) ? print "ok\n" : print "nok\n";
    > }
    > __END__
    > '39 ': nok
    > '0000 ': nok
    > '0139 ': nok
    > '000139': ok
    > ' 0139': nok
    >
    > Any regexp masters online?


    Do you need a single regex to give the whole answer? Otherwise

    /(\w+)( *)/ and length( $1) + length( $2) == $n;

    would do.

    If you need a ready-made regex, the only reasonable way I see is to
    construct it from a given $n:

    my $re = join '|', map '\\w' x $_ . ' ' x ( $n - $_), 1 .. $n;

    It's probably possible to come up with something using (??{ ....}),
    but it won't be pretty. I'm not going there.

    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, Sep 9, 2005
    #3
  4. Guest

    > > I want to create a regexp in order to match N word characters followed
    > > by M trailing space(s), where N+M=<a fixed number>, N>1, M>=0.

    >
    > I'm reading the condition as "N >= 1, M >= 0". If you actually mean
    > "N > 1" the modifications are simple.


    You are correct.

    > Do you need a single regex to give the whole answer?


    Unfortunately, yes.

    > my $re = join '|', map '\\w' x $_ . ' ' x ( $n - $_), 1 .. $n;


    Excellent! Thanks Anno!

    > It's probably possible to come up with something using (??{ ....}),
    > but it won't be pretty. I'm not going there.


    Anyone? :)

    --
    Andreas
     
    , Sep 9, 2005
    #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. Brad Marts
    Replies:
    3
    Views:
    412
    Brad Marts
    Nov 17, 2003
  2. Donald Canton

    remove trailing whitespace from string

    Donald Canton, Feb 9, 2004, in forum: C++
    Replies:
    5
    Views:
    11,207
    Tilman Kuepper
    Feb 9, 2004
  3. KevinGPO
    Replies:
    1
    Views:
    342
    Richard Herring
    Feb 20, 2006
  4. Dotan Cohen
    Replies:
    2
    Views:
    514
    Dotan Cohen
    Oct 10, 2011
  5. Joao Silva
    Replies:
    16
    Views:
    363
    7stud --
    Aug 21, 2009
Loading...

Share This Page