在Python中使用正则表达式查找数据

2 投票
4 回答
2046 浏览
提问于 2025-04-16 09:59

我刚开始学习Python,也在学习开发。让我举个例子,说明我想做的事情。

我想找到一段文本,内容是name="username" type="hidden" value="blah",然后我只想提取出“blah”这个值。

我该从哪里开始呢?

4 个回答

0

其他人已经给出了很好的例子,展示了如何使用Python标准库中的re模块,但你也可以考虑使用Python的通用字符串处理方法。这样做不需要import,通常被认为更符合Python的风格。

示例行:

name="username" type="hidden" value="blah"

# given a file of the example line
for line in open('my_file.txt'):
    # split on the spaces in the line
    for item in line.split():
            # check if this is the 'value' attribute you need
            if 'value' in item:
                print item.split('"')[1]
2

可能像这样:

string = 'name="username" type="hidden" value="blah"'
#get the text between the quotes that is lead by an equal sign and a non whitespace character.
regex = re.compile('\S="([^"]+)"')
print regex.findall(string)

这里有一些很棒的资源,可以帮助你学习Python中的正则表达式:

3

你可以使用 正则表达式中的分组 来提取匹配结果中相关的部分。

#!/usr/bin/env python

s = """ Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
commodo consequat. 
name="username" type="hidden" value="blah" 
Duis aute irure dolor in reprehenderit in voluptate velit
esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat
non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
"""

import re

pattern = re.compile(r'name="username"\stype="hidden"\svalue="([^"]*)"')
for match in pattern.finditer(s):
    print match.group(1)
    # => blah

撰写回答