Python正则表达式语句没有返回正确的结果

时间:2016-05-13 21:34:24

标签: python regex

我的正则表达式语法没有返回正确的结果。我使用github3.py库从GitHub返回数据,该库在解析md文件(https://developer.github.com/v3/pulls/#list-pull-requests-files)的补丁密钥时返回三个可能的字符串。我已经阅读了正则表达式文档和几个主题,但我在语法中遗漏了一些东西。

string2 = '-> [HELP.SELECTOR]'
string3 = '+> [HELP.SELECTOR]'
string2

我想打印True表示与string3string1的完全匹配,如果找到string2,则打印为False。如果找到string3for prs in repo.pull_requests(): search_string_found = 'False' regex_search_string1 = re.compile(r"^\+>\s\[HELP.SELECTOR\]") regex_search_string2 = re.compile(r"^->\s\[HELP.SELECTOR\]") for data in repo.pull_request(prs.number).files(): match_text1 = regex_search_string1.search(data.patch) match_text2 = regex_search_string2.search(data.patch) if match_text1 is not None and match_text2 is not None: search_string_found = 'True' break print('HELP.SELECTOR present in file: ', search_string_found) ,我的结果将返回False。

{{1}}

2 个答案:

答案 0 :(得分:1)

由于您确认您的字符串可能不在字符串start处,因此您需要

regex_search_string = re.compile(r"[+-]>\s\[HELP\.SELECTOR\]")
for data in repo.pull_request(prs.number).files():
    match_text = regex_search_string.search(data.patch)
    if match_text:
        search_string_found = 'True'
        break

注意:

  • [+-]匹配+-,因为它是与其中指定的范围/集中的单个字符匹配的字符类
  • {li> + [...]-无需永久转义
  • [...] re.search的开头或结尾不必转义
  • None返回匹配数据对象或[0-9]+{1,3},您需要先检查它才能访问匹配/已捕获的文本

答案 1 :(得分:0)

维护一个正则表达式字符串比几个更容易。试试这个:

import re

strings = [
     '> [HELP.SELECTOR]$',
     '-> [HELP.SELECTOR]$',
     '+> [HELP.SELECTOR]$',
]

for string in strings:
    print (bool(re.match(r'[-+]> \[HELP.SELECTOR\]$', string)), string)

结果:

False > [HELP.SELECTOR]
True -> [HELP.SELECTOR]
True +> [HELP.SELECTOR]

将其应用于您的问题,

#UNTESTED
for prs in repo.pull_requests():
    search_string_found = any(
        re.match(r'[-+]> \[HELP.SELECTOR\]', data.patch)
        for data in repo.pull_request(prs.number).files())
    print('HELP.SELECTOR present in file: ', search_string_found)