[英]Regex to match the first element of the last line
我有一個格式如下的表格:
# HEADER
1.452000000E-08 9.999999956E+00
2.407483818E-06 9.999999956E+00
1.096959505E-05 9.999999956E+00
2.215696736E-05 9.999999961E+00
3.187463656E-05 9.999999977E+00
4.121852471E-05 1.000000002E+01
4.981256454E-05 1.000000009E+01
5.768279362E-05 1.000000017E+01
6.495080199E-05 1.000000026E+01
7.173653894E-05 1.000000035E+01
7.813611913E-05 1.000000044E+01
8.422150607E-05 1.000000053E+01
9.004513021E-05 1.000000062E+01
9.564467570E-05 1.000000071E+01
1.010469937E-04 1.000000079E+01
1.062711251E-04 1.000000087E+01
1.113306207E-04 1.000000095E+01
1.162353387E-04 1.000000103E+01
1.209928265E-04 1.000000110E+01
1.256093412E-04 1.000000116E+01
1.300905579E-04 1.000000123E+01
1.344420136E-04 1.000000128E+01
1.386693389E-04 1.000000134E+01
1.427783330E-04 1.000000138E+01
1.467749408E-04 1.000000143E+01
我不知道確切的行數,但我想匹配最后一行的第一個元素(理想情況下,我也會為具有更多列的文件獲取其他元素)。 在這種情況下,它是數字1.467749408E-04
。
我可以將最后一行與^[[:blank:]][-+]?\d*\.?\d+[eE][-+]?\d+(.*)\z
或第一列與^[[:blank:]][-+]?\d*\.?\d+[eE][-+]?\d+
,但我不知道如何只獲取第一個元素,或者做一個 AND在這兩種情況下都有效。
您可以嘗試在全點模式下使用此正則表達式模式:
^.*(\d+\.\d+E[+-]\d+)\s+\d+\.\d+E[+-]\d+$
該模式的工作原理如下:
^ from the start of the text
.* consume everything, across lines, until reaching
(\d+\.\d+E[+-]\d+) the second to last number (capture this in $1)
\s+ followed by whitespace
\d+\.\d+E[+-]\d+ the last number
$ the end of the text
您嘗試定位的號碼將在第一個捕獲組中可用。 您將如何執行此正則表達式並獲取捕獲組將取決於您使用的語言/工具。
這是一個應該可以工作的 Pythons 腳本:
inp = """# HEADER
1.452000000E-08 9.999999956E+00
...
1.467749408E-04 1.000000143E+01"""
matches = re.findall(r'^.*(\d+\.\d+E[+-]\d+)\s+\d+\.\d+E[+-]\d+$', inp,
flags=re.DOTALL)
print(matches[0])
這打印:
1.467749408E-04
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.