简体   繁体   English

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

[英]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.

相关问题 将所有出现的内容打印在以x开头并以y结尾的字符串中 - Printing all occurrences in a string starting with x and ending with y 在以至少 3 个组开头的列表中查找连续和相同数字的最佳组合 - Finding best combinations of consecutive and same numbers in a list starting with a group of at least 3 用Python中的唯一ID替换所有出现的字符串(根据其起始索引和结束索引) - Replace all occurrences of a string (given their starting and ending index ) with a unique ID in Python 在列表中查找连续数字的开头和结尾 - Find the beginning and ending of consecutive numbers in a list 查找数组中3个连续数字的总和 - Finding the sum of 3 consecutive numbers in an array 在列表中查找连续数字的序列 - Finding sequence of consecutive numbers on lists 替换所有连续重复值的出现 - Replace all the occurrences of consecutive repeated pair of values 查找单词的所有出现+子字符串 - Finding all occurrences + substrings of a word 重写代码以生成包含从2开始的连续质数的所有数字,小于给定数字 - Rewriting code to generate all numbers containing consecutive primes starting at two, less than a given number 在.txt文件中查找包含成千上万个数字的数字,这些数字以一组特定的4个数字结尾 - Finding numbers in a .txt file of thousands of numbers ending in a specific set of 4 numbers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM