我有類似格式的字串
hello this is an example [ a b c ]
hello this is another example [ cat bird dog elephant ]
我想轉換成
hello this is an example [a,b,c]
hello this is another example [cat,bird,dog,elephant]
但我不明白如何創建一個正則運算式模式,該模式洗掉括號旁邊的任何空格,并用單個,.
如何創建這樣的模式?
我目前的嘗試是一系列正則運算式替換。
m = re.sub('\[\s ','[',s)
m = re.sub('\s \]',']',m)
m = re.sub('\s ',' ',m)
m = re.sub(r'\s(?=[^\[\]]*])', ",", m)
但是,有人對如何使其更高效或更清潔有任何建議嗎?
uj5u.com熱心網友回復:
我沒有設法用花哨的模式來做到這一點,但是這個小解決方法怎么樣。只需撰寫一個模式來查找括號之間的所有內容,然后單獨處理該字串。比如:用空格分割它,過濾空元素(從開頭和結尾的前導和尾隨空格),然后將它們重新組合成一個用逗號分隔的字串。您傳遞的修改后的字串并將其re.sub替換為括號之間的所有內容。
s1 = "hello this is an example [ a b c ]"
s2 = "hello this is another example [ cat bird dog elephant ]"
pattern = r"(?<=\[)(.*)(?=\])"
print(
re.sub(
pattern,
','.join(list(filter(None, re.split(r"\s ", re.search(pattern, s1).group(1)))))
, s1)
)
print(
re.sub(
pattern,
','.join(list(filter(None, re.split(r"\s ", re.search(pattern, s2).group(1)))))
, s2)
)
輸出:
hello this is an example [a,b,c]
hello this is another example [cat,bird,dog,elephant]
uj5u.com熱心網友回復:
在第一步中,您可以嘗試提取方括號之間的文本。代碼應該看起來更具可讀性...
foo = 'hello this is another example [ cat bird dog elephant ]'
# get everything between [ and ]
reg_get_between_square_brackets= re.compile(r'\[(.*)\]')
str_to_replace = reg_get_between_square_brackets.findall(foo)[0]
# replace spaces with coma
new_string = re.sub('\s ', ',', str_to_replace.strip()) # strip to remove beginning/ending white space
print(foo.replace(str_to_replace, new_string))
輸出:
hello this is another example [cat,bird,dog,elephant]
uj5u.com熱心網友回復:
以下是我的解決方案,添加了一些評論。
對于第二部分(用逗號替換方括號之間的空格,我寧愿使用 split() 和 join() - 正則運算式解決方案肯定會更慢。)
import re
str1 = 'hello this is an example [ a b c ]'
str2 = 'hello this is another example [ cat bird dog elephant ]'
# remove the SPACES near square brackets
str1 = re.sub(r'\[\s*(.*\S)\s*\]', r'[\1]', str1)
print(str1)
# replace the SPACES inside the square brackets until no replacement
old_str1 = ''
while old_str1 != str1:
old_str1 = str1
str1 = re.sub(r'\[(\S*)\s (.*)\]', r'[\1,\2]', str1, count=0)
print(str1)
str2 = re.sub(r'\[\s*(.*\S)\s*\]', r'[\1]', str2)
print(str2)
old_str2 = ''
while old_str2 != str2:
old_str2 = str2
str2 = re.sub(r'\[(\S*)\s (.*)\]', r'[\1,\2]', str2, count=0)
print(str2)
輸出
hello this is an example [a b c]
hello this is an example [a,b,c]
hello this is another example [cat bird dog elephant]
hello this is another example [cat,bird,dog,elephant]
uj5u.com熱心網友回復:
您可以使用帶有單個捕獲組的否定字符類,然后在組 1 中用單個逗號替換 1 個或多個空格,并將結果括在方括號之間。
\[\s*([^][]*?)\s*]
模式匹配:
\[匹配[\s*匹配可選的前導空白字符(捕獲組 1[^][]*?可選地重復除[and以外的字符],盡可能少
)關閉組 1\s*]從字面上匹配
查看帶有捕獲組值的正則運算式演示和Python 演示。
import re
strings = [
"hello this is an example [ a b c ]",
"hello this is another example [ cat bird dog elephant ]"
]
pattern = r"\[\s*([^][]*?)\s*]"
for s in strings:
print(re.sub(pattern, lambda m: "[{0}]".format(re.sub(r"\s ", ',', m.group(1))), s))
輸出
hello this is an example [a,b,c]
hello this is another example [cat,bird,dog,elephant]
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/528902.html
標籤:Python正则表达式
