简体   繁体   English

python正则表达式:如何只过滤特殊字符?

[英]python regular expression : How can I filter only special characters?

I want to check either given words contain special character or not. 我想检查给定的单词是否包含特殊字符。
so below is my python code 所以下面是我的python代码

The literal 'a@bcd' has '@', so it will be matchd and it's ok. 文字'a @ bcd'有'@',所以它会匹配,没关系。
but 'a1bcd' has no special character. 但'a1bcd'没有特殊的性格。 but it was filtered too!! 但它也被过滤了!!

import re
regexp = re.compile('[~`!@#$%^&*()-_=+\[\]{}\\|;:\'\",.<>/?]+')

if regexp.search('a@bcd') :
    print 'matched!! nich catch!!'

if regexp.search('a1bcd') :
    print 'something is wrong here!!!'

result : python ../special_char.py matched!! 结果:python ../special_char.py匹配!! nich catch!! nich抓住!! something is wrong here!!! 这里不对劲!!!

I have no idea why it works like above..someone help me..T_T;;; 我不知道为什么它像上面那样工作..有人帮我... T_T ;;; thanks~ 谢谢〜

Move the dash in you regular expression to the start of the [] group, like this: 将正则表达式中的短划线移动到[]组的开头,如下所示:

regexp = re.compile('[-~`!@#$%^&*()_=+\[\]{}\\|;:\'\",.<>/?]+')

Where you had the dash, it was read with the surrounding characters as )-_ and since it is inside [] it is interpreted as asking to match a range from ) to _ . 你有破折号的地方,它被周围的字符读取为)-_因为它在[]里面,它被解释为要求匹配从)_的范围。 If you move the dash to just after the [ it has no special meaning and instead matches itself. 如果你将短划线移动到[它没有特殊含义之后,而是匹配自己。

Here's an interactive session showing the specific problem there was in your regular expression: 这是一个交互式会话,显示正则表达式中的特定问题:

>>> import re
>>> print re.search('[)-_]', 'abcd')
None
>>> print re.search('[)-_]', 'a1b')
<_sre.SRE_Match object at 0x7f71082247e8>
>>> print re.search('[)-_]', 'a1b').group(0)
1

After fixing it: 修好之后:

>>> print re.search('[-)_]', 'a1b')
None

Unless there's some reason not visible in your question, I'd also say that the final + is not needed. 除非你的问题中没有显示某些原因,否则我也会说不需要最后的+

re will be relatively slow for this re将是相对缓慢的

I'd suggest trying 我建议尝试

specialchars = '''-~`!@#$%^&*()_=+[]{}\\|;:'",.<>/?'''
len(word) != len(word.translate(None, specialchars))

or 要么

set(word) & set(specialchars)

暂无
暂无

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

相关问题 带有特殊字符的Python正则表达式 - Python Regular Expression with special characters 如何在Python中使用正则表达式删除具有特殊字符串的字符? - How to remove characters with special strings using regular expression in Python? python正则表达式也匹配特殊字符 - python regular expression also match special characters 如何编写正则表达式以查找字符组合,但每个字符只能在python中出现一次 - How to write regular expression to find combination of characters, but each can only appear once in python 如何使用正则表达式搜索字符串以查找包含字母,特殊字符(如-,())的字符串(使用python) - How to search string using regular expression for string contains characters alphabets and special characters like -, () using python 如何在Python中的正则表达式中仅使用重复字符组描述字符串 - How to describe string with only repeated characters groups in regular expression in Python 需要一个可以验证带有特殊字符(连字符、撇号等...)的名称的 python 正则表达式 - Need a python regular expression that can verify names with special characters(Hyphens, apostrophes, etc...) 通过python只捕获正则表达式中的某些字符 - capturing only certain characters in regular expression by python 我该如何在python中编写一个正则表达式,该正则表达式在字符串的第一个句点停止,该字符串包含不可预测的字符类型? - How can I write a regular expression in python that stops at the first period in a string, which has unpredictable kinds of characters? 如何格式化此python正则表达式? - How can I format this python regular expression?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM