2 回答

TA貢獻(xiàn)1995條經(jīng)驗(yàn) 獲得超2個(gè)贊
我想出了一個(gè)正則表達(dá)式解決你的問題。我盡量將正則表達(dá)式模式保持為“通用”,因?yàn)槲也恢牢谋局惺欠窨倳袚Q行符和空格,這意味著該模式選擇了很多空格,然后將其刪除。
#Import the module for regular expressions
import re
#Text to search. I CORRECTED IT A BIT AS YOUR EXAMPLE SAID second d AND second_c WAS FOLLOWED BY TWO COMMAS. I am assuming those were typos.
text = '''ENGINE = CollapsingMergeTree (
first_param
,(
second_a
,second_b, second_c
,second_d), third, fourth)'''
#Regex search pattern. re.S means . which represents ANY character, includes \n (newlines)
pattern = re.compile('ENGINE = CollapsingMergeTree \((.*?),\((.*?)\),(.*?), (.*?)\)', re.S) #ENGINE = CollapsingMergeTree \((.*?),\((.*?)\), (.*?), (.*?)\)
#Apply the pattern to the text and save the results in variable 'result'. result[0] would return whole text.
#The items you want are sub-expressions which are enclosed in parentheses () and can be accessed by using result[1] and above
result = re.match(pattern, text)
#result[1] will get everything after theparenteses after CollapsingMergeTree until it reaches a , (comma), but with whitespace and newlines. re.sub is used to replace all whitespace, including newlines, with nothing
first = re.sub('\s', '', result[1])
#result[2] will get second a-d, but with whitespace and newlines. re.sub is used to replace all whitespace, including newlines, with nothing
second = re.sub('\s', '', result[2])
third = re.sub('\s', '', result[3])
fourth = re.sub('\s', '', result[4])
print(first)
print(second)
print(third)
print(fourth)
輸出:
first_param
second_a,second_b,second_c,second_d
third
fourth
正則表達(dá)式解釋:\ = 轉(zhuǎn)義控制字符,這是一個(gè)正則表達(dá)式會解釋為特殊含義的字符。更多在這里。
\( = 轉(zhuǎn)義括號
() = 將括號中的表達(dá)式標(biāo)記為子組。見結(jié)果[1]等。
. = 匹配任何字符(包括換行符,因?yàn)?re.S)
* = 匹配前面表達(dá)式的 0 次或多次出現(xiàn)。
? = 匹配前面表達(dá)式的 0 或 1 次出現(xiàn)。
筆記: *?組合被稱為非貪婪重復(fù),這意味著前面的表達(dá)式只匹配一次,而不是一遍又一遍。
我不是專家,但我希望我的解釋是正確的。
我希望這有幫助。

TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超11個(gè)贊
對于任何語言,您可能想要使用適當(dāng)?shù)慕馕銎鳎ㄒ虼瞬檎胰绾问謩?dòng)滾動(dòng)解析器以用于簡單語言),但是由于您在此處顯示的一小部分看起來與 Python 兼容,因此您可以將其解析為如果是 Python 使用ast
模塊(來自標(biāo)準(zhǔn)庫)然后操作結(jié)果。
添加回答
舉報(bào)