繁体   English   中英

查找以空格开头和结尾的所有出现的 7 个连续数字

[英]Finding all occurrences of 7 consecutive numbers starting and ending with space

我需要使用正则表达式在 python 中找到所有以空格开头和结尾的 7 个连续数字。 如果字符串以一组 7 个数字开头,则必须省略 set之前的空格。 如果字符串以 7 个数字的集合结尾,则必须省略集合的空格。

str = "1234567 7777777 88888888  9999999   -7654321 555-5555 456456 123.1245  6666666"

预期 output:

["1234567", "7777777", "9999999", "6666666"]

例如

lst = re.findall("\d{7}", str)

只需在(?<?\S)\d{7}(?!\S)上找到所有内容:

str = "1234567 7777777 88888888  9999999   -7654321 555-5555 456456 123.1245  6666666"
nums = re.findall(r'(?<!\S)\d{7}(?!\S)', str)
print(nums)  # ['1234567', '7777777', '9999999', '6666666']

这里使用的正则表达式模式说:

(?<!\S)  assert that whitespace or start of string precedes
\d{7}    match a 7 digit number
(?!\S)   assert that whitespace or the end of the string follows

暂无
暂无

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

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