Python匹配xx xxxx数字-结果不准确

Python匹配xx xxxx数字-结果不准确,python,regex,string,Python,Regex,String,我的正则表达式很弱 我正在尝试匹配一个字符串,该字符串可能如下所示: 12-1234 *string* 12 1234 *string* 或 只要在给定字符串中找到该模式,它就应该通过 我想这应该足够了: a = re.compile("^\d{0,2}[\- ]\d{0,4}$") if a.match(dbfull_address): continue 但我仍然得到了不准确的结果: 12串 我想我的正则表达式需要帮助:D ^\d{0,2}[\- ]\d

我的正则表达式很弱

我正在尝试匹配一个字符串,该字符串可能如下所示:

12-1234 *string*

12 1234 *string*

只要在给定字符串中找到该模式,它就应该通过

我想这应该足够了:

    a = re.compile("^\d{0,2}[\- ]\d{0,4}$")

    if a.match(dbfull_address):
        continue
但我仍然得到了不准确的结果:

12串

我想我的正则表达式需要帮助:D

^\d{0,2}[\- ]\d{0,4}$
允许空格/破折号周围为零位,因此您可能希望使用
\d{1,2}[-]\d{1,4}

此外,您应该删除
$
锚定,除非您只想匹配第二个数字后面没有任何内容的行


由于Python的
.match()
方法将正则表达式匹配隐式地锚定到字符串的开头,因此也不需要锚定
^

到目前为止,这似乎工作得很好。谢谢你的帮助。你想要的图案是什么?在字符串的开头,在分隔符(可能是空格或破折号)之前可以有1-2位,在分隔符之后可以有1-4位,之后可以有任何内容?没有任何解释的代码不是很好的答案。“你能解释一下为什么你的解决方案有效吗?”布莱亚诺克利解释道。
reobj = re.compile(r"^[\d]{0,2}[\s\-]+[\d]{0,4}.*?$", re.IGNORECASE | re.MULTILINE)



Options: dot matches newline; case insensitive; ^ and $ match at line breaks

Assert position at the beginning of a line (at beginning of the string or after a line break character) «^»
Match a single digit 0..9 «[\d]{2}»
   Exactly 2 times «{2}»
Match the regular expression below and capture its match into backreference number 1 «(\.|\*)?»
   Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
   Match either the regular expression below (attempting the next alternative only if this one fails) «\.»
      Match the character “.” literally «\.»
   Or match regular expression number 2 below (the entire group fails if this one fails to match) «\*»
      Match the character “*” literally «\*»
Match the regular expression below and capture its match into backreference number 2 «([\d]{2})?»
   Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
   Match a single digit 0..9 «[\d]{2}»
      Exactly 2 times «{2}»
reobj = re.compile(r"^[\d]{0,2}[\s\-]+[\d]{0,4}.*?$", re.IGNORECASE | re.MULTILINE)



Options: dot matches newline; case insensitive; ^ and $ match at line breaks

Assert position at the beginning of a line (at beginning of the string or after a line break character) «^»
Match a single digit 0..9 «[\d]{2}»
   Exactly 2 times «{2}»
Match the regular expression below and capture its match into backreference number 1 «(\.|\*)?»
   Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
   Match either the regular expression below (attempting the next alternative only if this one fails) «\.»
      Match the character “.” literally «\.»
   Or match regular expression number 2 below (the entire group fails if this one fails to match) «\*»
      Match the character “*” literally «\*»
Match the regular expression below and capture its match into backreference number 2 «([\d]{2})?»
   Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
   Match a single digit 0..9 «[\d]{2}»
      Exactly 2 times «{2}»