开发者

How do I detect symbols in a string using regular expressions?

I am developing a web application for a school and the have things like class_name and course_name. A course_name is the parent of a class_name, hence the class_name must contain the course_name. For instance:

course_name = "Weeklies"
class_name = "Weeklies talks with Superstars"

The above case would be perfect and correct.

However, there are time开发者_运维知识库s a user will create a course_name with symbols that mess up the word boundary regular expression I have set for class_name ( \b\b ). Having the metacharacters of regular expressions in a course_name makes the word boundary return False everytime since symbols are not words.

QUESTION:

How do I check if a course_name contains symbols/metacharacters in Python and if it does, I want to return False else True?

-Mark


re.escape()


This is sort of a general purpose solution... Just replace the regular expression with a suitable grammar.

if not re.match(str, r'[\w\d\ ]+'):
    # valid
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜