【发布时间】:2020-03-29 20:54:48
【问题描述】:
由于我想将列表 (url_key) 中字符串的每个元素作为正则表达式来识别另一个列表中的元素是否具有模式,因此我需要为每个元素的所有非单词字符添加反斜杠在url_key 中使用python。
我的代码示例:
import re
sentences = ["Disallow DCCP sockets due to such NFC-3456",
"Check at http://www.n.io/search?query=title++sub/file.html",
"Specifies the hash algorithm on them"]
url_key = ['www.n.io/search?query=title++sub', 'someweb.org/dirs.io'] # there are thousands of elements
add_key = ['NFC-[0-9]{4}', 'CEZ-[0-9a-z]{4,8}'] # there are hundreds of elements
pattern = url_key + add_key
mykey = re.compile('(?:% s)' % '|'.join(pattern))
for item in sentences:
if mykey.search(item):
print (item, ' --> Keyword is found')
else:
print (item, ' --> Keyword is not Found')
但是这段代码给了我一个错误:
error Traceback (most recent call last)
<ipython-input-80-5348ee9c65ec> in <module>()
8
9 pattern = url_key + add_key
---> 10 mykey = re.compile('(?:% s)' % '|'.join(pattern))
11
12 for item in sentences:
~/anaconda3/lib/python3.6/re.py in compile(pattern, flags)
231 def compile(pattern, flags=0):
232 "Compile a regular expression pattern, returning a pattern object."
--> 233 return _compile(pattern, flags)
234
235 def purge():
~/anaconda3/lib/python3.6/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:
~/anaconda3/lib/python3.6/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
~/anaconda3/lib/python3.6/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
~/anaconda3/lib/python3.6/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
~/anaconda3/lib/python3.6/sre_parse.py in _parse(source, state, verbose, nested, first)
763 sub_verbose = ((verbose or (add_flags & SRE_FLAG_VERBOSE)) and
764 not (del_flags & SRE_FLAG_VERBOSE))
--> 765 p = _parse_sub(source, state, sub_verbose, nested + 1)
766 if not source.match(")"):
767 raise source.error("missing ), unterminated subpattern",
~/anaconda3/lib/python3.6/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
~/anaconda3/lib/python3.6/sre_parse.py in _parse(source, state, verbose, nested, first)
617 if item[0][0] in _REPEATCODES:
618 raise source.error("multiple repeat",
--> 619 source.tell() - here + len(this))
620 if sourcematch("?"):
621 subpattern[-1] = (MIN_REPEAT, (min, max, item))
error: multiple repeat at position 31
预期结果:
Disallow DCCP sockets due to such NFC-3456 --> Keyword is found
Check at http://www.n.io/search?query=title++sub/file.html --> Keyword is found
Specifies the hash algorithm on them --> Keyword is not found
任何帮助将不胜感激。谢谢。
【问题讨论】:
-
您能解释一下为什么需要这样做吗?这听起来像an XY problem 您正在尝试手动破解,而现有的字符串转义 API 可以解决问题(不一定按照您描述的方式行事)。此外,说“没用”也不是很有帮助。 minimal reproducible example 应该显示观察到的输出(如果引发异常,包括回溯)。
-
我完全同意@ShadowRanger。小心点,慢慢来,不要因为“它有效”就跳上去。
标签: python regex python-3.x str-replace