簡體   English   中英

Python如何匹配正則表達式模式

[英]Python how to match regular expression pattern

我想解析日志並使用正則表達式模式找到以下行,例如

r"(  *)"C-R-A-R(  *).( *)"

哪個不起作用,如何編寫這個正則表達式模式? 關鍵是找到CRAR ,然后是一些用空格分隔的數字(應該是子字符串)。 請注意每個之間的空格是幾個空格,而不是一個空格。

[0]:      C-R-A-R              4                 1              85.4        86.1        90.8        76.1        92.3          0.000       0.000" 

如果我們考慮這個測試數據:

text = """C-R-A-R              4                 1              85.4        86.1        90.8        76.1        92.3          0.000       0.000
B-D-D-D 0                    0  1 1 2
"""

您想提取第一行,而不是第二行,因為它不是以 CRAR 開頭的(我理解正確嗎?)

試試這個正則表達式

import re

pattern = re.compile(r'( *)(C-R-A-R)(?P<digits>[ \d\.]+)')

在每一行上應用模式:

matches = [pattern.search(line) for line in text.split('\n')]

只保留匹配的行:

matched_lines = [m for m in matches if m is not None]

你得到:

print(matched_lines)
>>> [<re.Match object; span=(0, 133), match='C-R-A-R              4                 1         >]

然后,如果需要,您可以提取字符串的數字部分進行處理,使用組名digits (使用語法定義?P<digits>

digits = matched_lines[0].group('digits').strip()

print(digits)

>>> '4                 1              85.4        86.1        90.8        76.1        92.3          0.000       0.000'



暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM