[英]Matching multiple patterns in a string
我有一個看起來像這樣的字符串:
s = "[A] text [B] more text [C] something ... [A] hello"
基本上它由[X] chars
,我試圖在每個[X]
之后獲取文本。
我想產生這個字典(我不在乎順序):
mydict = {"A":"text, hello", "B":"more text", "C":"something"}
我正在考慮使用正則表達式,但我不確定這是否是正確的選擇,因為在我的情況下,[A]、[B] 和 [C] 的順序可以改變,所以這個字符串也有效:
s = "[A] hello, [C] text [A] more text [B] something"
我不知道如何正確提取字符串。 任何人都可以指出我正確的方向嗎? 謝謝。
不確定這是否正是您要查找的內容,但由於重復而失敗
s = "[A] hello, [C] text [A] more text [B] something"
results = [text.strip() for text in re.split('\[.\]', s) if text]
letters = re.findall('\[(.)\]', s)
dict(zip(letters, results))
{'A': 'more text', 'B': 'something', 'C': 'text'}
由於輸出如下所示:
In [49]: results
Out[49]: ['hello,', 'text', 'more text', 'something']
In [50]: letters
Out[50]: ['A', 'C', 'A', 'B']
要解決重復問題,您可以執行以下操作....
mappings = {}
for pos, letter in enumerate(letters):
try:
mappings[letter] += ' ' + results[pos]
except KeyError:
mappings[letter] = results[pos]
這給出: {'A': 'hello, more text', 'B': 'something', 'C': 'text'}
更新
或者更好的是,您可以考慮使用默認字典:如下所示:在此處輸入鏈接描述
預期輸出:
mydict = {"A":"text, hello", "B":"more text", "C":"something"}
import re
s = "[A] text [B] more text [C] something ... [A] hello"
pattern = r'\[([A-Z])\]([ a-z]+)'
items = re.findall(pattern, s)
output_dict = {}
for x in items:
if x[0] in output_dict:
output_dict[x[0]] = output_dict[x[0]] + ', ' + x[1].strip()
else:
output_dict[x[0]] = x[1].strip()
print(output_dict)
>>> {'A': 'text, hello', 'B': 'more text', 'C': 'something'}
這是一個簡單的解決方案:
#!/usr/bin/python
import re
s = "[A] text [B] more text [C] something ... [A] hello"
d = dict()
for x in re.findall(r"\[[^\]+]\][^\[]*",s):
m = re.match(r"\[([^\]*])\](.*)",x)
if not d.get(m.group(1),0):
#Key doesn't already exist
d[m.group(1)] = m.group(2)
else:
d[m.group(1)] = "%s, %s" % (d[m.group(1)], m.group(2))
print d
印刷:
{'A': ' text , hello', 'C': ' something ... ', 'B': ' more text '}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.