python - 提取特定模式后的字符串并存储它们

标签 python regex file-io pattern-matching

我有一堆输出,看起来像:

 004400:  0x10000000 (268435456)
 004404:  0x0f010000 (251723776)
 004408:  0x0c018000 (201424896)
 00440c:  0x0c019000 (201428992)
 004410:  0x0b01a000 (184655872)
 004414:  0x0901a800 (151103488)
 004418:  0x0701aa00 (117549568)
 00441c:  0x0701aa80 (117549696)
 004420:  0x0701ab00 (117549824)
 004424:  0x0701ab80 (117549952)
 004428:  0x0701ac00 (117550080)
   .          .           .
   .          .           .
   .          .           .
 0047f4:  0x00000000 (0)
 0047f8:  0x00000000 (0)
 0047fc:  0x00000000 (0)

所以我想提取地址(第一列)之后的第二列中的内容(例如:0x10000000)。稍后我需要将它们写回,因此如果可以将它们存储在文件中然后作为列表读回,那就更好了。我是 Python 新手,想要找到一个易于使用的库。一些例子会很棒。非常感谢。

最佳答案

从索引 1 获取返回第二列的匹配组。

(?<=:)\s*(0x.*?\b)

Demo

<小时/>

也试试这个

[^:]\s*(0x.*?\b)

Demo

描述:

(?<=:)     Positive Lookbehind to match the character : literally
[^:]       match a single character that is not :
\s*        match any white space character 
.*?        matches any character (except newline) lazily
\b         assert position at a word boundary

示例代码:

import re
p = re.compile(ur'[^:]\s*(0x.*?\b)')
test_str = ...

re.findall(p, test_str)
<小时/>

看看这个 demo以及对所有列进行分组。

关于python - 提取特定模式后的字符串并存储它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24662469/

相关文章:

正则表达式匹配两个字符串之间所有字符的最后一次出现

c - 在 C 语言中使用 fopen 打开文件

c# - IntPtr.Zero 是否等同于 null?

Python - Pandas 数据框 - 通过列迭代

regex - Swift - 正则表达式匹配引号中的任何内容

python - 无法卸载 'numpy'

python - 删除分隔文件中的嵌套换行符?

c++ - 删除文件内容无效

javascript - 如何在 Google Colab 中使用 Svelte

python - 在 elasticsearch 中更新索引时出现 RequestError