Regular Expression for 001-999

A

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?

bye Armin
 
S

Stephane CHAZELAS

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$/'
 
J

John W. Krahn

Armin said:
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
 
C

Christoffer Hammarström

Armin said:
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
 
J

John W. Krahn

Armin said:
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
 
J

Josef Moellers

Armin said:
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
 
A

Armin Gajda

Armin said:
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
 
G

Glenn Jackman

At 2006-02-07 05:35AM said:
Armin said:
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}$/
 
A

Aaron Baugher

Armin Gajda said:
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.
 
A

A. Sinan Unur

Armin Gajda said:
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
 
X

Xicheng

Armin said:
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
 
A

Andrew

A. Sinan Unur said:
Armin Gajda said:
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
 
X

Xicheng

Andrew said:
A. Sinan Unur said:
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



 
W

William James

Andrew said:
A. Sinan Unur said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top