简体   繁体   English

Python -- 正则表达式匹配模式或字符串结尾

[英]Python -- Regex match pattern OR end of string

import re
re.findall("(\+?1?[ -.]?\(?\d{3}\)?[ -.]?\d{3}[ -.]?\d{4})(?:[ <$])", "+1.222.222.2222<")

The above code works fine if my string ends with a "<" or space.如果我的字符串以“<”或空格结尾,则上面的代码可以正常工作。 But if it's the end of the string, it doesn't work.但如果它是字符串的结尾,它就不起作用。 How do I get +1.222.222.2222 to return in this condition:在这种情况下如何让 +1.222.222.2222 返回:

import re
re.findall("(\+?1?[ -.]?\(?\d{3}\)?[ -.]?\d{3}[ -.]?\d{4})(?:[ <$])", "+1.222.222.2222")

*I removed the "<" and just terminated the string. *我删除了“<”并终止了字符串。 It returns none in this case.在这种情况下,它不返回任何内容。 But I'd like it to return the full string -- +1.222.222.2222但我希望它返回完整的字符串——+1.222.222.2222

POSSIBLE ANSWER:可能的答案:

import re
re.findall("(\+?1?[ -.]?\(?\d{3}\)?[ -.]?\d{3}[ -.]?\d{4})(?:[ <]|$)", "+1.222.222.2222")

I think you've solved the end-of-string issue, but there are a couple of other potential issues with the pattern in your question:我认为您已经解决了字符串结尾问题,但是您的问题中的模式还有一些其他潜在问题:

  • the - in [ -.] either needs to be escaped as \- or placed in the first or last position within square brackets, eg [-. ] [ - [ -.]中的 - 需要转义为\-或放在方括号内的第一个或最后一个 position 中,例如[-. ] [-. ] or [.-] ; [-. ][.-] if you search for [] in the docs here you'll find the relevant info:如果您在此处的文档中搜索[] ,您将找到相关信息:
Ranges of characters can be indicated by giving two characters and separating them 
by a '-', for example [a-z] will match any lowercase ASCII letter, [0-5][0-9] will match
all the two-digits numbers from 00 to 59, and [0-9A-Fa-f] will match any hexadecimal
digit. If - is escaped (e.g. [a\-z]) or if it’s placed as the first or last character
(e.g. [-a] or [a-]), it will match a literal '-'.
  • you may want to require that either matching parentheses or none are present around the first 3 of 10 digits using (?:\(\d{3}\)?|\d{3}[-. ]?)您可能需要使用(?:\(\d{3}\)?|\d{3}[-. ]?)

Here's a possible tweak incorporating the above这是包含上述内容的可能调整

import re
pat = "^((?:\+1[-. ]?|1[-. ]?)?(?:\(\d{3}\) ?|\d{3}[-. ]?)\d{3}[-. ]?\d{4})(?:[ <]|$)"
print( re.findall(pat, "+1.222.222.2222") )
print( re.findall(pat, "+1(222)222.2222") )
print( re.findall(pat, "+1(222.222.2222") )

Output: Output:

['+1.222.222.2222']
['+1(222)222.2222']
[]

Maybe try:也许尝试:

import re
re.findall("(\+?1?[ -.]?\(?\d{3}\)?[ -.]?\d{3}[ -.]?\d{4})(?:| |<|$)", "+1.222.222.2222")
  • null matches any position, +1.222.222.2222 null匹配任何 position, +1.222.222.2222
  • matches space character, +1.222.222.2222匹配空格字符, +1.222.222.2222
  • < matches less-than sign character, +1.222.222.2222< <匹配小于号字符, +1.222.222.2222<
  • $ end of line, +1.222.222.2222 $行尾, +1.222.222.2222

You can also use regex101 for easier debugging.您还可以使用regex101来简化调试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM