简体   繁体   English

使用正则表达式匹配字符串

[英]Using regex to match string

I am trying to match all phone numbers from text. 我正在尝试匹配文本中的所有电话号码。

https://pythex.org/?regex=%5C%2B%3F(%5Cd*)%5Cs%3F%5C(%3F(%5Cd*)%5C)%3F%5Cs%3F(%5Cd*)%5B%5Cs-%5D%3F(%5Cd*)%5Cs%3F(%5Cd*)%5Cs%3F(%5Cd*)%5Cs%3F&test_string=(510)%20588-3915%0A%2B1%20(510)%20879-4700%0A%2B1(888)654-0143%0A%2B1(919)277-2172%0A%2B1(866)707-7709%0A%2B1(919)597-7014%0A%2B44%20(0)%2020%208435%206555%0A%2B44%20(0)%2020%208435%206555%0A%2B33%201%2070%2070%2096%2061%0A%2B41%20(44)%20595%2094%2001%0A%2B32%20(9)%20277%2094%2021%0A%2B34%20(0)%20931%20790%20659%0A045%204750666%0A%2B41%2044%20595%2094%2001%0A%2B31%20(0)%2020%20262%203824%20okay.2%0A%2B31%20478-511014%0A%2B32%209%20277%2094%2021%0A%2B91%20900%20133%205555&ignorecase=0&multiline=0&dotall=0&verbose=0 https://pythex.org/?regex=%5C%2B%3F(%5Cd*)%5Cs%3F%5C(%3F(%5Cd*)%5C)%3F%5Cs%3F(%5Cd*) %5B%5Cs-%5D%3F(%5CD *)%5CS%3F(%5CD *)%5CS%3F(%5CD *)%5CS%3F&test_string =(510)%20588-3915%0A%2B1%20 (510)%20879-4700%0A%2B1(888)654-0143%0A%2B1(919)277-2172%0A%2B1(866)707-7709%0A%2B1(919)597-7014%0A% 2B44%20(0)%2020%208435%206555%0A%2B44%20(0)%2020%208435%206555%0A%2B33%201%2070%2070%2096%2061%0A%2B41%20(44) %20595%2094%2001%0A%2B32%20(9)%20277%2094%2021%0A%2B34%20(0)%20931%20790%20659%0A045%204750666%0A%2B41%2044%20595%2094 %2001%0A%2B31%20(0)%2020%20262%203824%20okay.2%0A%2B31%20478-511014%0A%2B32%209%20277%2094%2021%0A%2B91%20900%20133% 205555&忽略大小写= 0&多行= 0&DOTALL = 0&冗长= 0

This regex works well for me. 这个正则表达式对我来说效果很好。 When I check on regex match website. 当我检查正则表达式匹配网站时。 But when I use in actual code, it gives me wrong result 但是当我在实际代码中使用它会给我错误的结果

>>> text = 'my phone is +31 478-511014 and +91 900 133 5555'
>>> mobile = re.findall(r'\+?(\d*)\s?\(?(\d*)\)?\s?(\d*)[\s-]?(\d*)\s?(\d*)\s?(\d*)\s?', text)
>>> mobile
[('', '', '', '', '', ''), ('', '', '', '', '', ''), ('', '', '', '', '', ''), ('', '', '', '', '', ''), ('', '', '', '', '', ''), ('', '', '', '', '', ''), ('', '', '', '', '', ''), ('', '', '', '', '', ''), ('', '', '', '', '', ''), ('', '', '', '', '', ''), ('', '', '', '', '', ''), ('', '', '', '', '', ''), ('31', '478', '', '511014', '', ''), ('', '', '', '', '', ''), ('', '', '', '', '', ''), ('', '', '', '', '', ''), ('', '', '', '', '', ''), ('91', '900', '133', '5555', '', ''), ('', '', '', '', '', '')]

Am I doing something wrong? 难道我做错了什么?

如果您遇到自己的问题,该工具应该可以工作:

[(+\d]\d[-\d\s()]*\d

it's working if you replace one of the * to + 如果您将*之一替换为+,则可以正常工作

mobile = re.findall(r'+?(\\d+)\\s?(?(\\d*))?\\s?(\\d*)[\\s-]?(\\d*)\\s?(\\d*)\\s?(\\d*)\\s?', text) mobile = re.findall(r'+?(\\ d +)\\ s?(?(\\ d *))?\\ s?(\\ d *)[\\ s-]?(\\ d *)\\ s?(\\ d *)\\ s?(\\ d *)\\ s?',文本)

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

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