第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會有你想問的

如何從文本中解析參數(shù)?

如何從文本中解析參數(shù)?

守著星空守著你 2021-12-26 10:11:07
我有一個(gè)看起來像這樣的文字:ENGINE = CollapsingMergeTree (    first_param    ,(        second_a        ,second_b, second_c,        ,second d), third, fourth)引擎可以不同(而不是CollapsingMergeTree,可以有不同的詞,ReplacingMergeTree、SummingMergeTree...)但文本的格式總是ENGINE = word()。“=”號周圍可以有空格,但不是強(qiáng)制性的。括號內(nèi)是幾個(gè)參數(shù),通常是一個(gè)單詞和逗號,但有些參數(shù)在括號中,如上例中的 second。換行符可以在任何地方。行可以以逗號、括號或其他任何形式結(jié)束。我需要提取n個(gè)參數(shù)(我不知道提前多少)。在上面的例子中,有 4 個(gè)參數(shù):第一個(gè) = first_paramsecond = (second_a, second_b, second_c, second_d) [帶括號的提取]第三 = 第三第四 = 第四如何使用python(正則表達(dá)式或其他任何東西)做到這一點(diǎn)?
查看完整描述

2 回答

?
拉風(fēng)的咖菲貓

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á)式只匹配一次,而不是一遍又一遍。


我不是專家,但我希望我的解釋是正確的。


我希望這有幫助。


查看完整回答
反對 回復(fù) 2021-12-26
?
慕尼黑8549860

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é)果。


查看完整回答
反對 回復(fù) 2021-12-26
  • 2 回答
  • 0 關(guān)注
  • 137 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號