gpt4 book ai didi

python - 如何从python中的字符串中提取 token ?

转载 作者:太空狗 更新时间:2023-10-30 02:19:07 24 4
gpt4 key购买 nike

我是 Python 的新手,我知道 Perl 可以做到这一点,但我还没有在 Python 中找到它。我想要做的是从一行中提取一个标记。

p = re.compile('555=\d+')
ID = p.findall(line)

其中line是一个文件中的单行,555=之后的数字应该被记录下来。但是,用这两行,我只能得到 555=1234567 之类的东西,我真正想要的是 1234567。任何人都可以提供帮助并提出解决方案吗?谢谢!

最佳答案

使用 () 来捕捉你想要的:

>>> p = re.compile('555=(\d+)')
>>> p.findall("555=1234567")
['1234567']

(...)

Matches whatever regular expression is inside the parentheses, and indicates the start and end of a group; the contents of a group can be retrieved after a match has been performed, and can be matched later in the string with the \number special sequence

ref

关于python - 如何从python中的字符串中提取 token ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32148801/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com