[英]Finding all occurrences of 7 consecutive numbers starting and ending with space
I need to find all occurrences of 7 consecutive numbers starting and ending with space in python with regex.我需要使用正则表达式在 python 中找到所有以空格开头和结尾的 7 个连续数字。 If the string starts with set of 7 numbers the space before set must be omitted.
如果字符串以一组 7 个数字开头,则必须省略 set之前的空格。 If the string ends with set of 7 numbers the space after set must be omitted.
如果字符串以 7 个数字的集合结尾,则必须省略集合后的空格。
str = "1234567 7777777 88888888 9999999 -7654321 555-5555 456456 123.1245 6666666"
Expected output:预期 output:
["1234567", "7777777", "9999999", "6666666"]
eg例如
lst = re.findall("\d{7}", str)
Just find all on (?<?\S)\d{7}(?!\S)
:只需在
(?<?\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']
The regex pattern used here says to:这里使用的正则表达式模式说:
(?<!\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.