|
|
Thanks really
-----Original Message-----
From: Rob Dixon [mailto:rob.dixon@xxxxxxx]
Sent: Tuesday, October 14, 2008 6:31 PM
To: Perl Beginners
Cc: Sayed, Irfan (Cognizant)
Subject: Re: Regarding reg. exp.
Irfan.Sayed@xxxxxxxxxxxxx wrote:
> From: Rob Dixon [mailto:rob.dixon@xxxxxxx]
>> Irfan.Sayed@xxxxxxxxxxxxx wrote:
>>>
>>> I have a string and I need to parse that string to check whether it
is
>>> in required format or not. I have a Perl script which ask for user
>>> input. I have mentioned in the Perl script that input should be in
the
>>> following format.
>>>
>>> For example:- 1,2,3 OR 1 2 3
>>>
>>> Which means that delimiter between these figures should be comma OR
>>> space. No any other character. I need all users to adhere that and
if
>>> they not then they should exit.
>>>
>>> Can somebody please give me reg. exp. which can be used to parse the
>>> string and check whether comma OR space is there or not as a
delimiter
>>> and it should contain only numeric not alphabets.
>>
>> It is very draconian to require exactly one space or comma as
>> separators. However this will do what you ask.
>>
>> Rob
>>
>>
>> use strict;
>> use warnings;
>>
>> my $re = qr/^
>> \d+
>> (?:
>> (?:,\d+)* | (?: \d+)*
>> )
>> $/x;
>>
>> chomp (my $input = <>);
>>
>> if ($input =~ $re) {
>> print "ok\n";
>> }
>> else {
>> print "invalid\n";
>> }
>
> if ($trig_np =~ m/\d,{1}\d|\d\s{1}\d/)
>
> this what I did.
That is useless. If I typed
--=NONSENSE9 9NONSENSE=--
it would be accepted as valid. My code with Ruud's correction is the
simplest
way to do what you describe. Like this
my $re = qr/^
\d+
(?:
(?:,\d+)* | (?:[ ]\d+)*
)
$/x;
If you expect only single-digit numbers then replace '\d+' with '\d'
throughout.
If you only expect 1 through 9 then replace them with [1-9] instead.
Rob
This e-mail and any files transmitted with it are for the sole use of the
intended recipient(s) and may contain confidential and privileged information.
If you are not the intended recipient, please contact the sender by reply
e-mail and destroy all copies of the original message.
Any unauthorised review, use, disclosure, dissemination, forwarding, printing
or copying of this email or any action taken in reliance on this e-mail is
strictly
prohibited and may be unlawful.
|
|