简体   繁体   English

如何使用正则表达式检测字符串中的符号?

[英]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.

相关问题 如何使用正则表达式处理这样的字符串? - How do I process a string such as this using regular expressions? 如何使用 Python 中的正则表达式检测以空格分隔的数字? - How do I detect digit separated by space using regular expressions in Python? 如何使用正则表达式删除python字符串中的十六进制值? - How do I remove hex values in a python string with regular expressions? 如何使用正则表达式将字符串与python中的数字匹配? - How do I match a string up to a number in python using regular expressions? 如何使用python正则表达式将String数据附加到某些位置? - How do I append String data to certain positions using python regular expressions? 如何使用 Python 中的正则表达式删除以“@”开头并以空白字符结尾的字符串? - How do I remove a string that starts with '@' and ends with a blank character by using regular expressions in Python? 我如何使用正则表达式拆分此字符串 - How can i split this string using regular expressions 如何使用正则表达式找到最短的重叠匹配? - How do I find the shortest overlapping match using regular expressions? 如何使用Python中的正则表达式在页面中搜索文本? - How do I search for text in a page using regular expressions in Python? 如何在python中使用正则表达式形成单独的块? - How do I form separate blocks using regular expressions in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM