首页 > 解决方案 > 我正在做一个 re.findall 一个字符串,我得到一个错误:错误:在位置 0 没有重复

问题描述

下午好,我在使用 paramiko 通过团队进行 ssh 查询后得到以下字符串,我在变量“buff_config”中得到字符串,我想知道表达式“* B:P79COL01 # $”的次数从中找到,但这样做我得到以下错误:

/usr/local/lib/python3.6/re.py in findall(pattern, string, flags) 220 221 结果中包含空匹配。""" --> 222 return _compile(pattern, flags).findall(字符串)223 224 def finditer(模式,字符串,标志=0):

/usr/local/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:

/usr/local/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模式 = 无

/usr/local/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 除了详细:857 # VERBOSE 标志在模式内打开。成为

/usr/local/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而不是项目))417如果不是sourcematch(“|”):418中断

/usr/local/lib/python3.6/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("没什么可重复的", --> 616 source.tell() - here + len(this)) 617 if item[0][0] in _REPEATCODES: 618 raise source.error("多次重复”,

错误:在位置 0 没有可重复的内容

我使用的代码如下:

re.findall('*B:P79COL01#$',buff_config)

buff_config 变量:

在此处输入图像描述

对不起,但我可以放置 buff_config 变量包含的内容,因为我没有离开问题编辑器

标签: python

解决方案


*在正则表达式中的意思是“重复前面的内容 0 次或更多次”,所以它不能是第一个字符,这是错误试图告诉你的。

您可以转义它 ( \*) 以搜索文字*字符,但您甚至不需要正则表达式。你可以只使用buff_config.count('*B:P79COL01#').


推荐阅读