Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.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 re,如何匹配匹配字符串的未知部分?_Python_Regex - Fatal编程技术网

使用Python re,如何匹配匹配字符串的未知部分?

使用Python re,如何匹配匹配字符串的未知部分?,python,regex,Python,Regex,我在Python中使用re.search()来检测匹配,现在我想从这个匹配中提取信息 以下是字符串的示例: text = """... Number: 124 Name: Some "nice" Name Description: Some description which can also include characters like ?,.,!, etc. ...""" 看看

我在Python中使用
re.search()
来检测匹配,现在我想从这个匹配中提取信息

以下是字符串的示例:

text = """...
    Number: 124
    Name: Some "nice" Name
    Description: Some description which can also include characters like ?,.,!, etc.
    ..."""
看看这个例子,如果我不知道数据的样子,我如何提取有关
Name
Description
的信息?我只知道
Name:
Description:
后面跟着信息

我正在使用此代码检测匹配:

>>> match = re.search('Name:\s*.*',text,re.IGNORECASE)
>>> match.group()
'Name: Some "nice" Name'
如何从
'Name:\s*.
中提取
*.
部分?

仅供参考
编号:124
我可以轻松解决,因为我知道要提取什么-
\d+

>>> match = re.search('Number:\s*\d+',text,re.IGNORECASE)
>>> number = re.search('\d+',match.group(),re.IGNORECASE)

在括号内使用组匹配。然后您可以根据组号进行匹配。在您的情况下,您不需要问号,因为匹配者在行尾之前贪婪地尝试匹配是没有问题的

>>> match = re.search('Name:\s(.*\s?) ',text,re.IGNORECASE)
>>> print(match.group(1))
Some "nice" Name

谢谢!!!我太快了,不知怎的错过了那部分文档。。。