Re: to parse a string



Tassilo v. Parseval wrote:
> Also sprach Tad McClellan:
>
>>cc <mmzzcc.c@xxxxxxxxx> wrote:
>>
>>>Given a string, e.g. "01010123", any sample code to scan it and then
>>>find the "2" and "3" are not qualified in the string which requires
>>>only "0" and "1"?
>>
>>The usual idiom for validating data is:
>>
>> anchor the beginning.
>>
>> anchor the ending.
>
> Why the anchoring?
>
>> write a regex in between that accounts for everything
>> that you want to allow.
>>
>>
>>print "'$str' is bad data\n" unless $str =~ /^[01]+$/;
>
> Or use tr:
>
> print "'$str' is bad data\n" if $str =~ tr/^01//c;
^
Are you sure that caret is supposed to be there?


John
--
use Perl;
program
fulfillment
.



Relevant Pages

  • Re: to parse a string
    ... > Given a string, e.g. "01010123", any sample code to scan it and then ... The usual idiom for validating data is: ... anchor the beginning. ...
    (comp.lang.perl.misc)
  • Re: to parse a string
    ... >> Also sprach Tad McClellan: ... >>> anchor the beginning. ... >>> write a regex in between that accounts for everything ... "Reply" at the bottom of the article headers. ...
    (comp.lang.perl.misc)
  • Re: to parse a string
    ... Also sprach Tad McClellan: ... > The usual idiom for validating data is: ... > anchor the beginning. ...
    (comp.lang.perl.misc)