[英]How to extract digits from string and return the last 3 or less using regex
[英]how to change digits in a string using regex
我有一個字符串像..
'1.5"x3"x10" hey 7" x 4"x 2" how 9.5" x 9.5" x 7.5" are 7.1"x 4"x 2" you ..and rest of our conversation
我想要的是將字符串轉換成..
'1.5x3x10 hey 7x4x2 how 9.5x9.5x7.5 are 7.1x4x2 you.. and rest of our conversation
簡而言之,刪除數字之間的空格和"
。
我試圖通過做找到模式。
stuff = re.findall('(\d+\.\d+|\d+)?["]\s?x\s?(\d+\.\d+|\d+)?["]\s?x\s?(\d+\.\d+|\d+)?["]',strings)
print sub
它返回我
[('1.5', '3', '10'), ('7', '4', '2'), ('9.5', '9.5', '7.5'), ('7.1', '4', '2')]
所以我嘗試了
stuff = re.findall('\d+["]\s?x\s?\d+["]\s?x\s?\d+["]',strings)
print stuff
它返回我
['5"x3"x10"', '7" x 4"x 2"', '1"x 4"x 2"']
它不包含任何數字..我如何將我的字符串轉換為所需的數字? 有什么幫助嗎?
如果您真的想一步一步做,就必須對所有情況進行多次前瞻/后顧之憂(這是所有問題都被這個案例捕獲的一個問題):
import re
my_str = '\'1.5"x3"x10" hey 7" x 4"x 2" how 9.5" x 9.5" x 7.5" are 7.1"x 4"x 2" you ..and rest of our conversation'
mod_str = re.sub(r'(?<=[\dx])["\s]+(?=[x\s])|(?<=x)\s(?=\d)', '', my_str)
print(mod_str)
讓您:
'1.5x3x10 hey 7x4x2 how 9.5x9.5x7.5 are 7.1x4x2 you ..and rest of our conversation
如果將其分為多個步驟,可能會更快(更容易捕獲異常值)。
說明:
這里有兩種搜索模式, (?<=[\\dx])["\\s]+(?=[x\\s])
和(?<=x)\\s(?=\\d)
,它們是分開的by |
表示一個或另一個(以從左到右的方式,因此如果第一個組捕獲了一部分內容,則第二組將不會在其上執行)。
首先:
(?<= positive non-capturing lookbehind, capture the next segment only if match
[\dx]) match a single digit (0-9) or the 'x' character
)
["\s]+ match one or more " characters or whitespace
(?= positive non-capturing lookahead, capture the previous segment only if match
[x\s] match a single whitespace or 'x' character
)
第二:
(?<= positive non-capturing lookbehind, capture the next segment only if match
x match the 'x' character
)
\s match a single whitespace
(?= positive non-capturing lookahead, capture the previous segment only if match
\d match a single digit (0-9)
)
前者負責選擇數字周圍的空格和引號,而后一種則擴展了選擇“ x”字符周圍的空格的能力,只有在其后跟數字以增加第一個模式的不足之處。 它們一起匹配正確的引號和空格,然后使用re.sub()
方法將其替換為空字符串。
zwer顯然是regex的高手。 但是,您可能對替代方法感興趣,該方法有時可以使用更簡單的表達式。 它涉及使用re
模塊來標識要更改的字符串,然后使用Python函數進行操作。
在這種情況下,我們要識別帶小數或不帶小數的數字,始終后跟"
和"
, x
有時在一個或多個空格之前或之后。此代碼使用帶有備用表達式的正則表達式查找兩者,並將查找到的內容傳遞給replacer
並保留此功能可丟棄不需要的字符。
>>> import re
>>> quest = '1.5"x3"x10" hey 7" x 4"x 2" how 9.5" x 9.5" x 7.5" are 7.1"x 4"x 2" you ..and rest of our conversation'
>>> def replacer(matchobj):
... for group in matchobj.groups():
... if group:
... return group.replace(' ', '').replace('"', '')
...
>>> re.sub(r'([0-9\.]+\")|(\s*x\s*)', replacer, quest)
'1.5x3x10 hey 7x4x2 how 9.5x9.5x7.5 are 7.1x4x2 you ..and rest of our conversation'
sub
的Python文檔中的詳細信息。
我在這里不會太復雜。
我只一次匹配一組尺寸,然后替換空白和雙引號。
(\\d+(?:\\.\\d+)?(?:\\s*"\\s*x\\s*\\d+(?:\\.\\d+)?){2}\\s*")
展開式
( # (1 start)
\d+
(?: \. \d+ )?
(?:
\s* " \s* x \s*
\d+
(?: \. \d+ )?
){2}
\s* "
) # (1 end)
Python演示http://rextester.com/HUIYP80133
Python代碼
import re
def repl(m):
contents = m.group(1)
return re.sub( r'[\s"]+','', contents )
str = '\'1.5"x3"x10" hey 7" x 4"x 2" how 9.5" x 9.5" x 7.5" are 7.1"x 4"x 2" you ..and rest of our conversation'
newstr = re.sub(r'(\d+(?:\.\d+)?(?:\s*"\s*x\s*\d+(?:\.\d+)?){2}\s*")', repl, str)
print newstr
輸出量
'1.5x3x10 hey 7x4x2 how 9.5x9.5x7.5 are 7.1x4x2 you ..and rest of our conversation
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.