Python 检查两个加起来等于10的整数之间是否出现三个问号

Python 检查两个加起来等于10的整数之间是否出现三个问号,python,Python,新的正则表达式。下面的正则表达式有三个或更多的问号,它们是连续出现的,但是如果我想得到,如果两个数字之间有三个问号,加起来等于10,那么不管它们是否连续出现 import re def has_three_question_marks(word): regex_pattern = re.compile(r"(\d\D+)(?=(\d))") question_marks = re.compile(r"\d(\D+)?\?{3,}(\D+)?") matches = r

新的正则表达式。下面的正则表达式有三个或更多的问号,它们是连续出现的,但是如果我想得到,如果两个数字之间有三个问号,加起来等于10,那么不管它们是否连续出现

import re


def has_three_question_marks(word):
    regex_pattern = re.compile(r"(\d\D+)(?=(\d))")
    question_marks = re.compile(r"\d(\D+)?\?{3,}(\D+)?")
    matches = re.findall(regex_pattern, word)
    if matches:
        for match in matches:
            question_mark_matches = re.findall(question_marks, str(match))
            if question_mark_matches:
                if int(match[0][0]) + int(match[1]) == 10:
                    return True
                continue
    return False
has_three_question_marks("arrb6???4xxbl5???eee5") #This returns True
has_three_question_marks("acc?7??sss?3rr1??????5") #This returns False, I need it to return True
更好的方法(更早期的方法):


我不太清楚正则表达式是否是你实现这一点的工具。正则表达式无法检测重叠模式:

'1?1?1?1' --> false
您可以这样做:

import re

test = ["arrb6???4xxbl5???eee5","acc?7??sss?3rr1??????5", 
        "?5?5?5?", "1???1", "1?1?1?1","1??2?", "1??2?2","1??23?2"]


def areThreeIn(text):
    patt = r"\d(\?+)\d"
    matches = re.findall(patt,text) 
    return sum(len(m) for m in matches) >= 3

for t in test:
    print(f"{t:<20} {areThreeIn(t)}")
你可以这样做。在回答这个问题之前,我会说,仅使用正则表达式是有实际限制的,对于足够复杂的输入,您可能需要编写文本解析代码,而不仅仅是正则表达式匹配。“加起来等于10的数字”作为一个更一般的说法是正则表达式所不能做到的;一个理论上不可能的更经典的问题是检查一个字符串是否有匹配数量的正确嵌套的开括号和闭括号

如果我说“如果开始分隔符是1,那么结束分隔符是9”,而不是“相加到10”,依此类推,那么我应该能够构建一个(复杂的!)正则表达式,如下所示:

#!/usr/bin/env python3
import re

# runs of things neither question mark nor digit
not_qmark_digit = r'[^?\d]*'
# contiguous blocks of non-digit with exactly three question mark
three_qmark = '(?:' + not_qmark_digit + r'\?){3}' + not_qmark_digit
# build a regexp matching 1...?.?.?...9 given the start digit
def delimited(start):
  end = 10-start
  return str(start) + three_qmark + str(end)
# make the 9 cases starting with each digit
cases = [delimited(n) for n in range(1, 10)]
# and join them as alternatives
pattern = '|'.join(cases)

x = re.compile(pattern)
print(x.findall("arrb6???4xxbl5???eee5"))
# ['6???4', '5???eee5']
print(x.findall("acc?7??sss?3rr1??????5"))
# ['7??sss?3']

正如@PatrickArtner善意地询问的那样,请提供一些示例,说明您试图通过正则表达式解析什么样的数据,以及您希望从数据中得到什么样的输出,也可能是一些不正确的数据,以更好地帮助您。@PatrickArtner希望这是更好的“两个数字之间出现三个问号”-正好3或
=3
?@PedroLobito sorry大于或等于
import re

test = ["arrb6???4xxbl5???eee5","acc?7??sss?3rr1??????5", 
        "?5?5?5?", "1???1", "1?1?1?1","1??2?", "1??2?2","1??23?2"]


def areThreeIn(text):
    patt = r"\d(\?+)\d"
    matches = re.findall(patt,text) 
    return sum(len(m) for m in matches) >= 3

for t in test:
    print(f"{t:<20} {areThreeIn(t)}")
arrb6???4xxbl5???eee5 True
acc?7??sss?3rr1??????5 True
?5?5?5?              False
1???1                True
1?1?1?1              False
1??2?                False
1??2?2               False # overlapping: would be True 
1??23?2              True
#!/usr/bin/env python3
import re

# runs of things neither question mark nor digit
not_qmark_digit = r'[^?\d]*'
# contiguous blocks of non-digit with exactly three question mark
three_qmark = '(?:' + not_qmark_digit + r'\?){3}' + not_qmark_digit
# build a regexp matching 1...?.?.?...9 given the start digit
def delimited(start):
  end = 10-start
  return str(start) + three_qmark + str(end)
# make the 9 cases starting with each digit
cases = [delimited(n) for n in range(1, 10)]
# and join them as alternatives
pattern = '|'.join(cases)

x = re.compile(pattern)
print(x.findall("arrb6???4xxbl5???eee5"))
# ['6???4', '5???eee5']
print(x.findall("acc?7??sss?3rr1??????5"))
# ['7??sss?3']