Efficient way to search for invalid characters in python

前端 未结 9 1453
萌比男神i
萌比男神i 2021-01-07 03:10

I am building a forum application in Django and I want to make sure that users dont enter certain characters in their forum posts. I need an efficient way to scan their whol

9条回答
  •  一个人的身影
    2021-01-07 04:04

    You have to be much more careful when using regular expressions - they are full of traps.

    in the case of [^<>/\{}[]~] the first ] closes the group which is probably not what you intended. If you want to use ] in a group it has to be the first character after the [ eg []^<>/\{}[~]

    simple test confirms this

    >>> import re
    >>> re.search("[[]]","]")
    >>> re.search("[][]","]")
    <_sre.SRE_Match object at 0xb7883db0>
    

    regex is overkill for this problem anyway

    def clean_topic_message(self):
        topic_message = self.cleaned_data['topic_message']
        invalid_chars = '^<>/\{}[]~`$'
        if (topic_message == ""):
            raise forms.ValidationError(_(u'Please provide a message for your topic'))
        if set(invalid_chars).intersection(topic_message):
            raise forms.ValidationError(_(u'Topic message cannot contain the following: %s'%invalid_chars))
        return topic_message
    

提交回复
热议问题