开发者

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

开发者 https://www.devze.com 2023-02-06 14:28 出处:网络
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 aclass_name, hence the class_name must contain the course_name.For ins

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

精彩评论

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