How to check if a string contains some characters??
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.
Hi, in pure python you can also write: # ------------------------------------------------- import re if re.search(r"[^A-Za-z0-9\.\-_]",string_name): raise Exception("illegal character here.") # ------------------------------------------------- However this is untested. But you get the idea ;) re is certainly faster then in-loops. Regards Tino "Spicklemire, Jerry" wrote:
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.
_______________________________________________ Zope maillist - Zope@zope.org http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )
Tino Wildenhain wrote:
Hi,
in pure python you can also write: # ------------------------------------------------- import re
if re.search(r"[^A-Za-z0-9\.\-_]",string_name): raise Exception("illegal character here.")
# -------------------------------------------------
However this is untested. But you get the idea ;) re is certainly faster then in-loops.
Regards Tino
"Spicklemire, Jerry" wrote:
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.
FYI: There is no dtml-while tag. You cannot iterate a string with dtml-in 8^( To do this in dtml, the best thing is probably something like: set a tokens property called allowed_chars on the folder executing the method (or the root). This property should have all the allowed characters in it separated by spaces. Then use this DTML (untested): <dtml-in expr="_.range(_.len(string))"> <dtml-unless expr="string[_['sequence-item']] in allowed_chars"> <dtml-raise InvalidString>Invalid String</dtml-raise> </dtml-unless> </dtml-in> The performance of this would be much less than using an external python method and regular expressions however. -- | Casey Duncan | Kaivo, Inc. | cduncan@kaivo.com `------------------>
At 3/12/01 04:39 PM, Tino Wildenhain wrote:
in pure python you can also write: # ------------------------------------------------- import re
if re.search(r"[^A-Za-z0-9\.\-_]",string_name): raise Exception("illegal character here.")
# -------------------------------------------------
However this is untested. But you get the idea ;) re is certainly faster then in-loops.
I didn't test it either, but note that "." is not a special character inside the [] pair and that a bare "-" as the first or last character stands for itself. So the string could be more clearly written (imho) as just: [^A-Za-z0-9._-] -- Dennis Nichols nichols@tradingconnections.com
Thank you very much. Placed in an external method works great. Bye, MArcello Dennis Nichols wrote:
At 3/12/01 04:39 PM, Tino Wildenhain wrote:
in pure python you can also write: # ------------------------------------------------- import re
if re.search(r"[^A-Za-z0-9\.\-_]",string_name): raise Exception("illegal character here.")
# -------------------------------------------------
However this is untested. But you get the idea ;) re is certainly faster then in-loops.
I didn't test it either, but note that "." is not a special character inside the [] pair and that a bare "-" as the first or last character stands for itself. So the string could be more clearly written (imho) as just:
[^A-Za-z0-9._-]
-- Dennis Nichols nichols@tradingconnections.com
-- ---------------------------------------- Marcello Lupo Project Manager Atis Srl Via dell'Artigliere 2 37014 Castelnuovo del Garda (VR) Tel. +39-0457570960 - Fax +39-0457571268 E-mail: lupo@atisworld.com http://www.atisworld.com -----------------------------------------
Marcello asked:
Is there a simple way to check if a string contain only this values: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.-_" and otherwise raise an error?
Why have all the responses so far been so excessively complex? This looks like an ideal candidate for string.translate. Input: <dtml-var input missing="not set"><br> <dtml-if input> <dtml-let valid="_.string.letters+_.string.digits + '.-_'" badchars="_.string.translate(input, _.string.maketrans(' ', ' '), valid)"> <dtml-if badchars> Sorry, you aren't allowed those characters (<dtml-var badchars html_quote>) <dtml-else>Nice string </dtml-if> </dtml-let> </dtml-if>
participants (6)
-
Casey Duncan -
Dennis Nichols -
duncan@rcp.co.uk -
Marcello Lupo -
Spicklemire, Jerry -
Tino Wildenhain