regular expression question

B

bayxarea-usenet

I am trying to test a string to see if it begins with a combinations of
possible 3 letter words plus exactly 5 digits and a . (leading spaces
are ok)

for instance

example: if I want to test -> abc,def,ghi

then

abc00000.
def00001.
ghi00002.

should all pass - but

jkl00003. would fail
abc0002. would fail
def00002 would fail

I know how to handle the leading spaces and I can use | to test eachc
of the possible letter combinations - but how do I handle this with the
5 digits if don't know what the digits are going to be?
Thanks,

John
John
 
G

Gunnar Hjalmarsson

I am trying to test a string to see if it begins with a combinations of
possible 3 letter words plus exactly 5 digits and a . (leading spaces
are ok)

I know how to handle the leading spaces and I can use | to test eachc
of the possible letter combinations - but how do I handle this with the
5 digits if don't know what the digits are going to be?

perldoc perlrequick
perldoc perlretut
perldoc perlre

Do some studying, give it a try, and come back here with some code if
you don't manage to figure it out that way.
 
A

Atlantis

I am trying to test a string to see if it begins with a combinations of
possible 3 letter words plus exactly 5 digits and a . (leading spaces
are ok)

for instance

example: if I want to test -> abc,def,ghi

then

abc00000.
def00001.
ghi00002.

should all pass - but

jkl00003. would fail
abc0002. would fail
def00002 would fail

I know how to handle the leading spaces and I can use | to test eachc
of the possible letter combinations - but how do I handle this with the
5 digits if don't know what the digits are going to be?
Thanks,

John
John

#!c:\perl\bin\perl
use strict;
use warnings;

my @Input = ("abc00000.", "def00001.", "ghi00002.", "jkl00003.", "abc0002.",
"def00002", "ghi00002..", "ghi0002..");

foreach my $Record (@Input)
{
if ($Record =~ /\s*(abc|def|ghi)\d{5}\.{1}\s*/) # Checks for leading and
trailing white spaces.
{
print "$Record is valid\n";
}
else
{
print "$Record is invalid\n";
}
}
 
G

Gunnar Hjalmarsson

Atlantis said:
if ($Record =~ /\s*(abc|def|ghi)\d{5}\.{1}\s*/)
# Checks for leading and trailing white spaces.

In what way does the regex check for leading and trailing white spaces?
Which strings would it match that are not matched by:

/(abc|def|ghi)\d{5}\./

(or vice versa) ??
 
A

Atlantis

Tad McClellan said:
^^^

Why do you include the {1} ?
No good reason other than just thought it added clarity to the statement.
(Obviously statement works without it too.)

Would probably revise the statement to look like...

if ($Record =~ /^(abc|def|ghi)\d{5}\.$/)
 
A

Atlantis

Gunnar Hjalmarsson said:
In what way does the regex check for leading and trailing white spaces?
Which strings would it match that are not matched by:

/(abc|def|ghi)\d{5}\./

(or vice versa) ??

You're quite right, have revised it to look like...

if ($Record =~ /^(abc|def|ghi)\d{5}\.$/)

Regards.
 
G

Gunnar Hjalmarsson

Atlantis said:
You're quite right, have revised it to look like...

if ($Record =~ /^(abc|def|ghi)\d{5}\.$/)

That does not do what the OP asked for.

"I am trying to test a string to see if it begins with a combinations of
possible 3 letter words plus exactly 5 digits and a . (leading spaces
are ok)"

This does:

/^\s*(abc|def|ghi)\d{5}\./
 
A

Atlantis

Gunnar Hjalmarsson said:
That does not do what the OP asked for.

"I am trying to test a string to see if it begins with a combinations of
possible 3 letter words plus exactly 5 digits and a . (leading spaces
are ok)"

This does:

/^\s*(abc|def|ghi)\d{5}\./

Right again:$;$
 

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
474,166
Messages
2,570,903
Members
47,446
Latest member
Pycoder

Latest Threads

Top