我有一堆输出,看起来像:
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)
<小时/>
也试试这个
[^:]\s*(0x.*?\b)
描述:
(?<=:) 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/