簡體   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