Regular Expression for 001-999

Discussion in 'Perl Misc' started by Armin Gajda, Feb 7, 2006.

  1. Armin Gajda

    Armin Gajda Guest

    Hi,

    can someone tell me how to define a regular expression that matches

    L001-L999 OR P001-P009?

    I found: ^[LP]\d{3}$

    But here the L000 and P000 is allowed.
    Any ideas?

    bye Armin
     
    Armin Gajda, Feb 7, 2006
    #1
    1. Advertising

  2. 2006-02-07, 10:32(+01), Armin Gajda:
    > Hi,
    >
    > can someone tell me how to define a regular expression that matches
    >
    > L001-L999 OR P001-P009?
    >
    > I found: ^[LP]\d{3}$
    >
    > But here the L000 and P000 is allowed.
    > Any ideas?

    [...]

    an extended regexp (for awk or grep -E):

    ^[LP]([0-9][0-9][1-9]|[0-9][1-9]0|[1-9][0-9]0)$

    but can't you use two regexps and check that one is matched and
    the other one is not like:

    POSIXLY_CORRECT=1 command -p awk '/^[LP][0-9]{3}$/ && ! /^[LP]000$/'


    --
    Stéphane
     
    Stephane CHAZELAS, Feb 7, 2006
    #2
    1. Advertising

  3. Armin Gajda wrote:
    >
    > can someone tell me how to define a regular expression that matches
    >
    > L001-L999 OR P001-P009?
    >
    > I found: ^[LP]\d{3}$
    >
    > But here the L000 and P000 is allowed.
    > Any ideas?


    /^[LP](?:00[1-9]|0[1-9]\d|[1-9]\d\d)$/



    John
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Feb 7, 2006
    #3
  4. Armin Gajda wrote:
    > Hi,
    >
    > can someone tell me how to define a regular expression that matches
    >
    > L001-L999 OR P001-P009?
    >
    > I found: ^[LP]\d{3}$
    >
    > But here the L000 and P000 is allowed.
    > Any ideas?
    >
    > bye Armin


    /^L(\d{3})$/ && $1 > 0 || /^P(\d{3})$/ && $1 > 0 && $1 < 10

    Kreiger
     
    Christoffer Hammarström, Feb 7, 2006
    #4
  5. Armin Gajda wrote:
    >
    > can someone tell me how to define a regular expression that matches
    >
    > L001-L999 OR P001-P009?
    >
    > I found: ^[LP]\d{3}$
    >
    > But here the L000 and P000 is allowed.
    > Any ideas?


    /^(?:[LP]00[1-9]|L(?:0[1-9]\d|[1-9]\d\d))$/


    John
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Feb 7, 2006
    #5
  6. Armin Gajda wrote:
    > Hi,
    >
    > can someone tell me how to define a regular expression that matches
    >
    > L001-L999 OR P001-P009?
    >
    > I found: ^[LP]\d{3}$
    >
    > But here the L000 and P000 is allowed.
    > Any ideas?


    Tricky.
    (Note: untested!)
    Let's try the second (easy) one first:
    ^P00[1-9]$
    The first one's more difficult:
    If the first character is [1-9], the second and third don't care
    If the first is 0 and the second is [1-9], the third doesn't care
    If the first and second are 0, the third must be 1-9:
    ^L(([1-9]\d\d)|(0[1-9]\d)|(00[1-9]))$

    HIRATH,

    Josef
    --
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett
     
    Josef Moellers, Feb 7, 2006
    #6
  7. Armin Gajda

    Armin Gajda Guest

    Armin Gajda schrieb:
    > Hi,
    >
    > can someone tell me how to define a regular expression that matches
    >
    > L001-L999 OR P001-P009?
    >
    > I found: ^[LP]\d{3}$
    >
    > But here the L000 and P000 is allowed.
    > Any ideas?
    >
    > bye Armin


    Thank you for the answers - it works!

    bye Armin
     
    Armin Gajda, Feb 7, 2006
    #7
  8. At 2006-02-07 05:35AM, Armin Gajda <> wrote:
    > Armin Gajda schrieb:
    > > Hi,
    > >
    > > can someone tell me how to define a regular expression that matches
    > >
    > > L001-L999 OR P001-P009?
    > >
    > > I found: ^[LP]\d{3}$
    > >
    > > But here the L000 and P000 is allowed.
    > > Any ideas?
    > >
    > > bye Armin

    >
    > Thank you for the answers - it works!
    >
    > bye Armin


    Also, negative look-ahead:
    /^[LP](?!000)\d{3}$/


    --
    Glenn Jackman
    Ulterior Designer
     
    Glenn Jackman, Feb 7, 2006
    #8
  9. Armin Gajda <> writes:

    > can someone tell me how to define a regular expression that matches
    >
    > L001-L999 OR P001-P009?
    >
    > I found: ^[LP]\d{3}$
    >
    > But here the L000 and P000 is allowed.


    If this is a practical situation and not a homework assignment, I'd
    stick with your simple regex, and skip those two special cases with a
    second simple regex, something like:

    ( /^[LP]\d{3}$/ and not /000/ )

    I don't know whether that'll run faster or slower, but it was
    certainly faster to code.


    --
    Aaron --
    http://360.yahoo.com/aaron_baugher
     
    Aaron Baugher, Feb 7, 2006
    #9
  10. Aaron Baugher <> wrote in
    news::

    > Armin Gajda <> writes:
    >
    >> can someone tell me how to define a regular expression that matches
    >>
    >> L001-L999 OR P001-P009?
    >>
    >> I found: ^[LP]\d{3}$
    >>
    >> But here the L000 and P000 is allowed.

    >
    > If this is a practical situation and not a homework assignment, I'd
    > stick with your simple regex, and skip those two special cases with a
    > second simple regex, something like:
    >
    > ( /^[LP]\d{3}$/ and not /000/ )
    >
    > I don't know whether that'll run faster or slower, but it was
    > certainly faster to code.


    You might want to look at the requirements again:

    L001-L999

    or

    P001-P009

    Hence, if you want a single regex:

    #!/usr/bin/perl

    use strict;
    use warnings;

    while (<DATA>) {
    chomp;
    print "$_\n" if m{ \A (?:L\d\d[1-9]) | (?:p00[1-9]) \z }x;
    }

    __DATA__
    L000
    L123
    L999
    P000
    P001
    P901

    Or, it might be faster to break it up:

    #!/usr/bin/perl

    use strict;
    use warnings;

    while (<DATA>) {
    chomp;
    print "$_\n" if m{ \A L \d \d [1-9] \z }x
    or m{ \A P 0 0 [1-9] \z }x;
    }

    __DATA__
    L000
    L123
    L999
    P000
    P001
    P901

    --
    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, Feb 7, 2006
    #10
  11. Armin Gajda

    Xicheng Guest

    Armin Gajda wrote:
    > Hi,
    >
    > can someone tell me how to define a regular expression that matches
    >
    > L001-L999 OR P001-P009?
    >
    > I found: ^[LP]\d{3}$
    >
    > But here the L000 and P000 is allowed.
    > Any ideas?

    for perl:

    ^(?:L\d\d|P00)[1-9]$

    Xicheng

    >
    > bye Armin
     
    Xicheng, Feb 7, 2006
    #11
  12. Armin Gajda

    Andrew Guest

    A. Sinan Unur wrote:
    > Aaron Baugher <> wrote in
    > news::
    >
    > > Armin Gajda <> writes:
    > >
    > >> can someone tell me how to define a regular expression that matches
    > >>
    > >> L001-L999 OR P001-P009?
    > >>
    > >> I found: ^[LP]\d{3}$
    > >>
    > >> But here the L000 and P000 is allowed.

    > >
    > > If this is a practical situation and not a homework assignment, I'd
    > > stick with your simple regex, and skip those two special cases with a
    > > second simple regex, something like:
    > >
    > > ( /^[LP]\d{3}$/ and not /000/ )
    > >
    > > I don't know whether that'll run faster or slower, but it was
    > > certainly faster to code.

    >
    > You might want to look at the requirements again:
    >
    > L001-L999
    >
    > or
    >
    > P001-P009
    >
    > Hence, if you want a single regex:
    >
    > #!/usr/bin/perl
    >
    > use strict;
    > use warnings;
    >
    > while (<DATA>) {
    > chomp;
    > print "$_\n" if m{ \A (?:L\d\d[1-9]) | (?:p00[1-9]) \z }x;
    > }
    >
    > __DATA__
    > L000
    > L123
    > L999
    > P000
    > P001
    > P901
    >
    > Or, it might be faster to break it up:
    >
    > #!/usr/bin/perl
    >
    > use strict;
    > use warnings;
    >
    > while (<DATA>) {
    > chomp;
    > print "$_\n" if m{ \A L \d \d [1-9] \z }x
    > or m{ \A P 0 0 [1-9] \z }x;
    > }
    >
    > __DATA__
    > L000
    > L123
    > L999
    > P000
    > P001
    > P901
    >


    The above does not match (decimal-)round nonzero data (L330, L500,
    etc.)

    If the OP actually did mean "P001-P009", perhaps a variation on Aaron
    Baugher's method:

    while($d=<DATA>) {
    chomp $d;
    print "\n$d : ", (&Match($d) ? 'YES!' : 'nope');
    }

    sub Match {
    ( ($_[0]=~/^(L(\d{3})|P00[1-9])$/) && ($2 !~/000/ ) );
    }

    __DATA__
    L000
    L123
    L999
    P000
    P001
    P901
    P400
    L570
    L119
    P109
    P2345
    L4500


    ---------output: --------------

    L000 : nope
    L123 : YES!
    L999 : YES!
    P000 : nope
    P001 : YES!
    P901 : nope
    P400 : nope
    L570 : YES!
    L119 : YES!
    P109 : nope
    P2345 : nope
    L4500 : nope


    Andrew




    > --
    > 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
     
    Andrew, Feb 7, 2006
    #12
  13. Armin Gajda

    Xicheng Guest

    Andrew wrote:
    > A. Sinan Unur wrote:
    > > Aaron Baugher <> wrote in
    > > news::
    > >
    > > > Armin Gajda <> writes:
    > > >
    > > >> can someone tell me how to define a regular expression that matches
    > > >>
    > > >> L001-L999 OR P001-P009?
    > > >>
    > > >> I found: ^[LP]\d{3}$
    > > >>
    > > >> But here the L000 and P000 is allowed.
    > > >
    > > > If this is a practical situation and not a homework assignment, I'd
    > > > stick with your simple regex, and skip those two special cases with a
    > > > second simple regex, something like:
    > > >
    > > > ( /^[LP]\d{3}$/ and not /000/ )
    > > >
    > > > I don't know whether that'll run faster or slower, but it was
    > > > certainly faster to code.

    > >
    > > You might want to look at the requirements again:
    > >
    > > L001-L999
    > >
    > > or
    > >
    > > P001-P009
    > >
    > > Hence, if you want a single regex:
    > >
    > > #!/usr/bin/perl
    > >
    > > use strict;
    > > use warnings;
    > >
    > > while (<DATA>) {
    > > chomp;
    > > print "$_\n" if m{ \A (?:L\d\d[1-9]) | (?:p00[1-9]) \z }x;
    > > }
    > >
    > > __DATA__
    > > L000
    > > L123
    > > L999
    > > P000
    > > P001
    > > P901
    > >
    > > Or, it might be faster to break it up:
    > >
    > > #!/usr/bin/perl
    > >
    > > use strict;
    > > use warnings;
    > >
    > > while (<DATA>) {
    > > chomp;
    > > print "$_\n" if m{ \A L \d \d [1-9] \z }x
    > > or m{ \A P 0 0 [1-9] \z }x;
    > > }
    > >
    > > __DATA__
    > > L000
    > > L123
    > > L999
    > > P000
    > > P001
    > > P901
    > >

    >
    > The above does not match (decimal-)round nonzero data (L330, L500,
    > etc.)
    >
    > If the OP actually did mean "P001-P009", perhaps a variation on Aaron
    > Baugher's method:


    right, i also forgot something like "L570", how about this one:

    m{^(?:L(\d\d\d)(?(?{+$1==0})(?!))|P00[1-9])$}

    Xicheng

    >
    > while($d=<DATA>) {
    > chomp $d;
    > print "\n$d : ", (&Match($d) ? 'YES!' : 'nope');
    > }
    >
    > sub Match {
    > ( ($_[0]=~/^(L(\d{3})|P00[1-9])$/) && ($2 !~/000/ ) );
    > }
    >
    > __DATA__
    > L000
    > L123
    > L999
    > P000
    > P001
    > P901
    > P400
    > L570
    > L119
    > P109
    > P2345
    > L4500
    >
    >
    > ---------output: --------------
    >
    > L000 : nope
    > L123 : YES!
    > L999 : YES!
    > P000 : nope
    > P001 : YES!
    > P901 : nope
    > P400 : nope
    > L570 : YES!
    > L119 : YES!
    > P109 : nope
    > P2345 : nope
    > L4500 : nope
    >
    >
    > Andrew
    >
    >
    >
    >
    > > --
    > > 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
     
    Xicheng, Feb 7, 2006
    #13
  14. Andrew wrote:
    > A. Sinan Unur wrote:
    > > Aaron Baugher <> wrote in
    > > news::
    > >
    > > > Armin Gajda <> writes:
    > > >
    > > >> can someone tell me how to define a regular expression that matches
    > > >>
    > > >> L001-L999 OR P001-P009?
    > > >>
    > > >> I found: ^[LP]\d{3}$
    > > >>
    > > >> But here the L000 and P000 is allowed.
    > > >
    > > > If this is a practical situation and not a homework assignment, I'd
    > > > stick with your simple regex, and skip those two special cases with a
    > > > second simple regex, something like:
    > > >
    > > > ( /^[LP]\d{3}$/ and not /000/ )
    > > >
    > > > I don't know whether that'll run faster or slower, but it was
    > > > certainly faster to code.

    > >
    > > You might want to look at the requirements again:
    > >
    > > L001-L999
    > >
    > > or
    > >
    > > P001-P009
    > >
    > > Hence, if you want a single regex:
    > >
    > > #!/usr/bin/perl
    > >
    > > use strict;
    > > use warnings;
    > >
    > > while (<DATA>) {
    > > chomp;
    > > print "$_\n" if m{ \A (?:L\d\d[1-9]) | (?:p00[1-9]) \z }x;
    > > }
    > >
    > > __DATA__
    > > L000
    > > L123
    > > L999
    > > P000
    > > P001
    > > P901
    > >
    > > Or, it might be faster to break it up:
    > >
    > > #!/usr/bin/perl
    > >
    > > use strict;
    > > use warnings;
    > >
    > > while (<DATA>) {
    > > chomp;
    > > print "$_\n" if m{ \A L \d \d [1-9] \z }x
    > > or m{ \A P 0 0 [1-9] \z }x;
    > > }
    > >
    > > __DATA__
    > > L000
    > > L123
    > > L999
    > > P000
    > > P001
    > > P901
    > >

    >
    > The above does not match (decimal-)round nonzero data (L330, L500,
    > etc.)
    >
    > If the OP actually did mean "P001-P009", perhaps a variation on Aaron
    > Baugher's method:
    >
    > while($d=<DATA>) {
    > chomp $d;
    > print "\n$d : ", (&Match($d) ? 'YES!' : 'nope');
    > }
    >
    > sub Match {
    > ( ($_[0]=~/^(L(\d{3})|P00[1-9])$/) && ($2 !~/000/ ) );
    > }
    >
    > __DATA__
    > L000
    > L123
    > L999
    > P000
    > P001
    > P901
    > P400
    > L570
    > L119
    > P109
    > P2345
    > L4500
    >
    >
    > ---------output: --------------
    >
    > L000 : nope
    > L123 : YES!
    > L999 : YES!
    > P000 : nope
    > P001 : YES!
    > P901 : nope
    > P400 : nope
    > L570 : YES!
    > L119 : YES!
    > P109 : nope
    > P2345 : nope
    > L4500 : nope


    Ruby:


    class String
    def ok
    match /^ ( L\d\d\d | P00\d ) $/x and !match /^.000/
    end
    end

    DATA.each { |x| x.chomp!
    puts "#{ x } : #{ x.ok ? 'YES!' : 'nope' }"
    }

    __END__
    L000
    L123
    L999
    P000
    P001
    P901
    P400
    L570
    L119
    P109
    P2345
    L4500
     
    William James, Feb 7, 2006
    #14
    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. VSK
    Replies:
    2
    Views:
    2,303
  2. Buddy Ackerman

    999 error

    Buddy Ackerman, Mar 3, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    3,382
    David Wang [Msft]
    Mar 4, 2006
  3. Michael B Allen

    Random Integers from 0 to 999

    Michael B Allen, Mar 24, 2005, in forum: C Programming
    Replies:
    36
    Views:
    1,058
    Grumble
    Apr 20, 2005
  4. I_have_nothing

    Any easy to printf an interger in 9,999, 99 format?

    I_have_nothing, May 13, 2005, in forum: C Programming
    Replies:
    4
    Views:
    404
    Chris McDonald
    May 13, 2005
  5. HAPPY NEW YEAR Funny Christmas wallpapers, theme F

    Funny Christmas wallpapers, theme & 15,999$/- per day

    HAPPY NEW YEAR Funny Christmas wallpapers, theme F, Dec 26, 2008, in forum: C Programming
    Replies:
    0
    Views:
    282
    HAPPY NEW YEAR Funny Christmas wallpapers, theme F
    Dec 26, 2008
Loading...

Share This Page