【问题标题】:How to display all matches in re.findall for a pattern which has string and number in python如何在 re.findall 中显示在 python 中具有字符串和数字的模式的所有匹配项
【发布时间】:2013-03-08 11:41:16
【问题描述】:

我想提取以下模式的所有匹配项:

pattern = 'link_uid=[0-9]'

我的字符串看起来像:

astr = 'this is test link_uid=23500534 and the second test is link_uid=12345'

我正在寻找以下输出:

link_uid=23500534
link_uid=12345

当我做re.findall('link_uid=[0-9]', astr) 我得到:link_uid=2, link_uid=1

【问题讨论】:

    标签: python regex search match


    【解决方案1】:

    你的输入包含多个数字,匹配那些:

    r'link_uid=\d+'
    

    结果:

    >>> re.findall(r'link_uid=\d+', astr)
    ['link_uid=23500534', 'link_uid=12345']
    

    【讨论】:

    • 我刚刚编辑了空间,没错,但我的示例中没有空间。 \s* 是空格吗?
    • @user89423:是的,\s* 匹配 0 个或多个空格。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-28
    • 1970-01-01
    相关资源
    最近更新 更多