擅长:python、mysql、java
<p>使用<a href="https://docs.python.org/3/library/re.html#re.match" rel="nofollow noreferrer">re.match()</a>和<a href="https://docs.python.org/3/library/re.html#re.match.groupdict" rel="nofollow noreferrer">match.groupdict()</a>(获取匹配的所有命名子组)方法的解决方案:</p>
<pre><code>s = '''
BPZ vs Griffin - League of Legends - Challenger Korea
Sat 2/25 1511 BPZ 1.645
10:30PM 1512 Griffin 2.250
'''
p = r'^(?P<event>[\w ]+-[\w ]+)\s[\w\s-]+' \
r'(?P<date>[A-Z]\w+ \d+\/\d{2})\s+\d+\s(?P<outcome1>[\w ]+)' \
r'(?P<outcome1odds>\d+\.\d+)\s+(?P<time>\d+:\d+(AM|PM))\s+\d+\s' \
r'(?P<outcome2>[\w ]+)(?P<outcome2odds>\d+\.\d+)'
matches = re.match(p, s.strip(), re.M)
result = {k:v.strip() for k,v in matches.groupdict().items()}
print(result)
</code></pre>
<p>输出:</p>
<pre><code>{'time': '10:30PM', 'event': 'BPZ vs Griffin - League of Legends', 'outcome2odds': '2.250', 'date': 'Sat 2/25', 'outcome2': 'Griffin', 'outcome1': 'BPZ', 'outcome1odds': '1.645'}
</code></pre>