我正在尝试删除字符串中的某些字符,但是我有一个错误,我有不同的方式循环但它仍然无法正常工作
我的问题是我如何循环字符串并删除字符?
这是我的代码:
MyList= [",", ":", "\"", "=", "&", ";", "%", "$","@", "%", "^",
"*", "(", ")", "{", "}","[", "]", "|", "/", "\\", ">",
"<", "-",'!', '?', '.', "'",'--', '---', "#"]
for remove in MyList:
mystring =re.sub(remove, "", "I am trying this code")
print (remove)
我有这个错误:
---------------------------------------------------------------------------
error Traceback (most recent call last)
<ipython-input-386-df22385a9f06> in <module>()
5 for remove in MyList:
6
----> 7 mystring =re.sub(remove, "", "I am trying this code ! ? /")
8 print (remove)
~\AppData\Local\Continuum\anaconda3\lib\re.py in sub(pattern, repl, string, count, flags)
189 a callable, it's passed the match object and must return
190 a replacement string to be used."""
--> 191 return _compile(pattern, flags).sub(repl, string, count)
192
193 def subn(pattern, repl, string, count=0, flags=0):
~\AppData\Local\Continuum\anaconda3\lib\re.py in _compile(pattern, flags)
299 if not sre_compile.isstring(pattern):
300 raise TypeError("first argument must be string or compiled pattern")
--> 301 p = sre_compile.compile(pattern, flags)
302 if not (flags & DEBUG):
303 if len(_cache) >= _MAXCACHE:
~\AppData\Local\Continuum\anaconda3\lib\sre_compile.py in compile(p, flags)
560 if isstring(p):
561 pattern = p
--> 562 p = sre_parse.parse(p, flags)
563 else:
564 pattern = None
~\AppData\Local\Continuum\anaconda3\lib\sre_parse.py in parse(str, flags, pattern)
853
854 try:
--> 855 p = _parse_sub(source, pattern, flags & SRE_FLAG_VERBOSE, 0)
856 except Verbose:
857 # the VERBOSE flag was switched on inside the pattern. to be
~\AppData\Local\Continuum\anaconda3\lib\sre_parse.py in _parse_sub(source, state, verbose, nested)
414 while True:
415 itemsappend(_parse(source, state, verbose, nested + 1,
--> 416 not nested and not items))
417 if not sourcematch("|"):
418 break
~\AppData\Local\Continuum\anaconda3\lib\sre_parse.py in _parse(source, state, verbose, nested, first)
614 if not item or (_len(item) == 1 and item[0][0] is AT):
615 raise source.error("nothing to repeat",
--> 616 source.tell() - here + len(this))
617 if item[0][0] in _REPEATCODES:
618 raise source.error("multiple repeat",
error: nothing to repeat at position 0
对不起我的新手问题
答案 0 :(得分:1)
您不需要正则表达式。只需使用str.replace
:
>>> MyList= [",", ":", "\"", "=", "&", ";", "%", "$","@", "%", "^",
... "*", "(", ")", "{", "}","[", "]", "|", "/", "\\", ">",
... "<", "-",'!', '?', '.', "'",'--', '---', "#"]
>>> mystring = "Helo, world!?"
>>> for s in MyList:
... mystring = mystring.replace(s, '')
...
>>> mystring
'Helo world'
答案 1 :(得分:1)
已经发布了一些解决方法,但还没有人解释错误。 re.sub()的文档说:
re.sub(pattern, repl, string, ...) - Return the string obtained by replacing ... pattern in string by the replacement repl
因此,在遍历列表中的一些符号后,我们得到了这个:
re.sub("*", "", "I am trying this code")
因此,您尝试将*
替换为""
- 但*
是正则表达式中使用的“特殊”字符,在这种情况下,"*"
是一个非法/无效的正则表达。 *
是正则表达式中的量词,意味着“尽可能多地返回前一个正则表达式的重复” - 但该字符串中没有先前的正则表达式。
"*"
通常与"."
中的".*"
一起使用,这意味着尽可能多地匹配任何单个字符("."
)("*"
)。
更新:这是我如何从字符串中删除符号:
import string
s = "I# am trying th<>is code!"
print(s.translate(None, string.punctuation))
答案 2 :(得分:0)
如果您的目标只是从字符串中删除所有标点符号,则保留所有其他字符(_
和包含空格)的单个正则表达式将为:
import re
s = "Hello, world! how are you?"
print(re.sub("[^ \w]","",s))
结果:
Hello world how are you
它比循环中的x替换调用(创建尽可能多的字符串)或带有|
的正则表达式更有效。
答案 3 :(得分:0)
这里有一个列表理解和加入的答案,不是最优雅,也不是最快:
my_list= [",", ":", "\"", "=", "&", ";", "%", "$","@", "%", "^",
"*", "(", ")", "{", "}","[", "]", "|", "/", "\\", ">",
"<", "-",'!', '?', '.', "'",'--', '---', "#"]
my_string = "Hello, world!?"
# one liner
my_string = ''.join([c for c in my_string if c not in my_list])
print(my_string)
答案 4 :(得分:-2)
您还可以将列表组合成单个表达式:
pattern = re.compile(r"\b(" + "|".join(MyList) + ")\\W")
pattern.sub("", mystring )