简体   繁体   English

如何使用正则表达式搜索字符串以查找包含字母,特殊字符(如-,())的字符串(使用python)

[英]How to search string using regular expression for string contains characters alphabets and special characters like -, () using python

I want to search dynamically some string contains case insensitive and some string contains special characters like (TM), (R) how to search those strings 我想动态搜索某些字符串不区分大小写,某些字符串包含特殊字符,例如(TM),(R)如何搜索那些字符串

with open(logfile) as inf:
                for line in inf:
                    if re.search(string,line,re.IGNORECASE):
                       # print 'found line',line

I wanted to add re.IGNORECASE,re.escape() are giving error, How to solve it 我想添加re.IGNORECASE,re.escape()给出错误,如何解决

If i put this into python console... 如果我将其放入python控制台...

>>> re.search(re.escape("Stack(R) Overflow"), "Hey bro, Stack(R) Overflow", re.I)
<_sre.SRE_Match object at 0xb778adb0>

..i got correct result. ..我有正确的结果。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Python 在带有正则表达式的字符串中搜索/查找特殊字符,如 &amp;、&lt; 或 &gt; - How to search/find special characters like &, < or > in the string with regex using Python 如何使用正则表达式python在字符串中搜索特殊字符的精确匹配 - How to search for an exact match of special characters in a string using regex python 如何在Python中使用正则表达式删除具有特殊字符串的字符? - How to remove characters with special strings using regular expression in Python? 如何在Python中编写一个接受字母,数字和一些选定的特殊字符(,.- |;!_?)的正则表达式? - How to write a regular expression in Python that accepts alphabets, numbers and a few selected special characters(,.-|;!_?)? 使用正则表达式从字符串中删除字符 - Using regular expression to remove characters from string 字符串的正则表达式包含字符是否全部用大写python表示 - Regular Expression for a string contains if characters all in capital python 使用正则表达式排除字符串搜索中的字符? - using regular expressions to exclude characters in a string search? 如何使用 python 的键盘模块键入任何包含特殊字符的给定字符串? - How to type any given string, which contains special characters, using python's keyboard module? Python 搜索字符串包含字符 - Python search string contains characters 检查字符串是否包含python中的特殊字符 - check if string contains special characters in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM