[Zope] How to check if a string contains some characters??
Spicklemire, Jerry
Jerry.Spicklemire@IFLYATA.COM
Mon, 12 Mar 2001 09:36:41 -0500
Marcello asked:
> Is there a simple way to check if a string contain only this values:
> "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.-_"
> and otherwise raise an error?
If you have a string you want to check, with the name "string_name":
<dtml-call "REQUEST.set('char_invalid', 'no')">
<dtml-let
chars_valid="'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
.-_'">
<dtml-while "char_valid == 'no'">
<dtml-in string_name>
<dtml-if "sequence-item !in chars_valid">
<dtml-call "REQUEST.set('char_invalid', 'yes')">
</dtml-if>
</dtml-in>
</dtml-while>
</dtml-let>
<dtml-if "char_invalid == 'no'">
<dtml-call "your raise_error_code">
</dtml-if>
This may not look as simple as you want, but in pure Python,
(without alls the <dtml-stuff > this would be:
string_valid = 'yes'
for char in string_name :
if char !in
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.-_' :
string_valid = 'no'
break
if string_valid == 'no' :
(run your "raise error" code here)
Later,
Jerry S.