当 finditer() 找不到任何东西时我想做点什么。
import re
pattern = "1"
string = "abc"
matched_iter = re.finditer(pattern, string)
# <if matched_iter is empty (no matched found>.
# do something.
# else
for m in matched_iter:
print m.group()
Run Code Online (Sandbox Code Playgroud)
我能想到的最好的办法就是手动跟踪发现:
mi_no_find = re.finditer(r'\w+',"$$%%%%") # not matching.
found = False
for m in mi_no_find:
print m.group()
found = True
if not found:
print "Nothing found"
Run Code Online (Sandbox Code Playgroud)
不回答的相关帖子:
[编辑]
- 我对枚举或计算总输出没有兴趣。仅当找到其他未找到的操作。
- 我知道我可以将 finditer 放入列表中,但这对于大字符串来说效率低下。目标之一是降低内存利用率。
用于re.search(pattern, string)检查模式是否存在。
pattern = "1"\nstring = "abc"\n\nif re.search(pattern, string) is None:\n print(\'do this because nothing was found\')\nRun Code Online (Sandbox Code Playgroud)\n\n返回:
\n\ndo this because nothing was found\nRun Code Online (Sandbox Code Playgroud)\n\n如果您想迭代返回,请将 放在re.finditer()内re.search()。
pattern = \'[A-Za-z]\'\nstring = "abc"\n\nif re.search(pattern, string) is not None:\n for thing in re.finditer(pattern, string):\n print(\'Found this thing: \' + thing[0])\nRun Code Online (Sandbox Code Playgroud)\n\n返回:
\n\nFound this thing: a\nFound this thing: b\nFound this thing: c\nRun Code Online (Sandbox Code Playgroud)\n\n因此,如果您想要这两个选项,请使用else:带有条件的子句if re.search()。
pattern = "1"\nstring = "abc"\n\nif re.search(pattern, string) is not None:\n for thing in re.finditer(pattern, string):\n print(\'Found this thing: \' + thing[0])\nelse:\n print(\'do this because nothing was found\')\nRun Code Online (Sandbox Code Playgroud)\n\n返回:
\n\ndo this because nothing was found\nRun Code Online (Sandbox Code Playgroud)\n\n如果 .finditer() 与模式不匹配,则它将不会在相关循环内执行任何命令。
\n\n所以:
\n\n这样,如果正则表达式调用没有返回任何内容,则循环将不会执行,循环后的变量调用将返回与设置的变量完全相同的变量。
\n\n下面的示例 1 演示了查找模式的正则表达式。示例 2 显示正则表达式未找到模式,因此循环内的变量从未设置。\n示例 3显示了我的建议 - 在正则表达式循环之前设置变量,因此如果正则表达式找不到匹配项(随后不会触发循环),则循环之后的变量调用将返回初始变量集(确认未找到正则表达式模式)。
\n\n记得导入import re模块。
\n\n示例 1(在字符串“hello world”中搜索字符“he”将返回“he”)
\n\nmy_string = \'hello world\'\npat = \'(he)\'\nregex = re.finditer(pat,my_string)\n\nfor a in regex:\n b = str(a.groups()[0])\nprint(b)\n\n# returns \'he\'\nRun Code Online (Sandbox Code Playgroud)\n\n示例 2(在字符串“hello world”中搜索字符“ab”与任何内容都不匹配,因此“for a in regex:”循环不会执行,并且不会为 b 变量分配任何值.)
\n\nmy_string = \'hello world\'\npat = \'(ab)\'\nregex = re.finditer(pat,my_string)\n\nfor a in regex:\n b = str(a.groups()[0])\nprint(b)\n\n# no return\nRun Code Online (Sandbox Code Playgroud)\n\n示例3(再次搜索字符\'ab\',但这次在循环之前将变量b设置为\'CAKE\',并在循环之外调用变量b,返回初始变量 - 即\' CAKE\' - 因为循环没有执行)。
\n\nmy_string = \'hello world\'\npat = \'(ab)\'\nregex = re.finditer(pat,my_string)\n\nb = \'CAKE\' # sets the variable prior to the for loop\nfor a in regex:\n b = str(a.groups()[0])\nprint(b) # calls the variable after (and outside) the loop\n\n# returns \'CAKE\'\nRun Code Online (Sandbox Code Playgroud)\n\n还值得注意的是,在设计要输入正则表达式的模式时,请确保使用括号来指示组的开始和结束。
\n\npattern = \'(ab)\' # use this\npattern = \'ab\' # avoid using this\nRun Code Online (Sandbox Code Playgroud)\n\n回到最初的问题:
\n\n由于没有发现\xe2\x80\x99t执行for循环(对于正则表达式中的a),因此用户可以预加载变量,然后在for循环之后检查它的原始加载值。这将使用户知道是否没有找到任何内容。
\n\nmy_string = \'hello world\'\npat = \'(ab)\'\nregex = re.finditer(pat,my_string)\n\nb = \'CAKE\' # sets the variable prior to the for loop\nfor a in regex:\n b = str(a.groups()[0])\nif b == \xe2\x80\x98CAKE\xe2\x80\x99:\n # action taken if nothing is returned\nRun Code Online (Sandbox Code Playgroud)\n