Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用正则表达式在Python中查找数据_Python_Regex - Fatal编程技术网

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

使用正则表达式在Python中查找数据,python,regex,Python,Regex,我是python新手,一般都在开发中。让我举一个我正在尝试做的例子 我想找到文本name=“username”type=“hidden”value=“blah”,我只想拉“blah” 我该怎么开始呢?类似这样的事情可能: string = 'name="username" type="hidden" value="blah"' #get the text between the quotes that is lead by an equal sign and a non whitespace c

我是python新手,一般都在开发中。让我举一个我正在尝试做的例子

我想找到文本name=“username”type=“hidden”value=“blah”,我只想拉“blah”


我该怎么开始呢?

类似这样的事情可能:

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中正则表达式的重要资源:

您可以使用来选择匹配的相关部分

#!/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

如果要将所有值放入字典,可以使用以下函数:

import re

def get_pair_map(s):
    map = {}
    pair_re = re.compile('(\w+)="(\w+)"')
    map.update(pair_re.findall(s))
    return map

其他人在Python的标准库中给出了使用<代码> Re>代码>模块的优秀例子,但是您也可以考虑使用Python的通用字符串处理。它避免了导入,通常被认为更“Pythonic”

示例行:

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]