[英]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. 我正在为学校开发Web应用程序,并且具有class_name和course_name之类的内容。 A course_name is the parent of a class_name, hence the class_name must contain the course_name. course_name是class_name的父项,因此class_name必须包含course_name。 For instance: 例如:
course_name = "Weeklies"
class_name = "Weeklies talks with Superstars"
The above case would be perfect and correct. 上述情况将是完美和正确的。
However, there are times 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 ). 但是,有时用户会创建带有带有混淆我为class_name(\\ b \\ b)设置的单词边界正则表达式的符号的course_name。 Having the metacharacters of regular expressions in a course_name makes the word boundary return False everytime since symbols are not words. 在course_name中包含正则表达式的元字符会使单词边界每次都返回False,因为符号不是单词。
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? 如何检查course_name是否包含Python中的符号/元字符,如果包含,我想返回False或True?
-Mark -标记
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.