Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.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.search()逻辑上或re.search()中的两种模式_Python_Regex_Findall - Fatal编程技术网

Python re.search()逻辑上或re.search()中的两种模式

Python re.search()逻辑上或re.search()中的两种模式,python,regex,findall,Python,Regex,Findall,我有下面的字符串 Page load for http://xxxx?roxy=www.yahoo.com&sendto=https://mywebsite?ent took 4001 ms (Ne: 167 ms, Se: 2509 ms, Xe: 1325 ms)<br><br><br>Topic: Yahoo!! My website is a good website | Mywebsite<br> 此正则表达式与第一部分匹配,并

我有下面的字符串

Page load for http://xxxx?roxy=www.yahoo.com&sendto=https://mywebsite?ent took 4001 ms (Ne: 167 ms, Se: 2509 ms, Xe: 1325 ms)<br><br><br>Topic: Yahoo!! My website is a good website | Mywebsite<br>
此正则表达式与第一部分匹配,并返回我4001。如何使用日志或表达式
r“\?ent\s*\take\s*太长\s*\(\d+)

要从第二部分中提取12343?

正则表达式开头的问号不跟随任何可以设置为可选的内容。如果要在此处匹配文字问号,请编写
\?

x = int(re.findall(r"\?ent\s*took\s*([^m]*)",message)[0])

正则表达式开头的问号不跟随任何可以设置为可选的内容。如果要匹配此处的文字问号,请编写
\?

x = int(re.findall(r"\?ent\s*took\s*([^m]*)",message)[0])

首先,您需要在模式的开头转义
,因为
标记是一个正则字符,使字符串成为可选的,并且必须在字符串之前!因此,如果您想对
进行数学运算,您还需要使用
\?
作为一种更有效的方法,您可以在中使用
重新搜索
\d+
您的模式,并拒绝额外的索引:

>>> int(re.search(r"\?ent\s*took\s*(\d+)",s).group(1))
4001
对于第二个示例,您可以执行以下操作:

>>> re.search(r'\((\d+)',s).group(1)
'12343'
对于这两种情况下的匹配,请使用以下模式:

(\d+)[\s\w]+\(|\((\d+)

>>s1=“页面加载http://xxxx?roxy=www.yahoo.com&sendto=https://mywebsite?ent 花了太长时间(12343毫秒Ne:167毫秒Se:2509毫秒Xe:1325毫秒)

主题:雅虎!!我的网站是个好网站|我的网站
“ >>>s2=“页面加载用于http://xxxx?roxy=www.yahoo.com&sendto=https://mywebsite?ent 拍摄了4001毫秒(Ne:167毫秒,Se:2509毫秒,Xe:1325毫秒)

主题:雅虎!!我的网站是一个好网站|我的网站
“ >>>重新搜索(r'(\d+[\s\w]+\(\124;\(\ d+),s1).组(2) '12343' >>>重新搜索(r'(\d+[\s\w]+\(\124;\(\ d+),s2).组(1) '4001'
首先,您需要在模式的开头转义
,因为
标记是一个正则字符,使字符串成为可选的,并且必须在字符串之前!因此,如果您想对
进行数学运算,您还需要使用
\?
作为一种更有效的方法,您可以使用
重新搜索
\d+/code>在您的模式中,并拒绝额外的索引:

>>> int(re.search(r"\?ent\s*took\s*(\d+)",s).group(1))
4001
对于第二个示例,您可以执行以下操作:

>>> re.search(r'\((\d+)',s).group(1)
'12343'
对于这两种情况下的匹配,请使用以下模式:

(\d+)[\s\w]+\(|\((\d+)

>>s1=“页面加载http://xxxx?roxy=www.yahoo.com&sendto=https://mywebsite?ent 花了太长时间(12343毫秒Ne:167毫秒Se:2509毫秒Xe:1325毫秒)

主题:雅虎!!我的网站是个好网站|我的网站
“ >>>s2=“页面加载用于http://xxxx?roxy=www.yahoo.com&sendto=https://mywebsite?ent 拍摄了4001毫秒(Ne:167毫秒,Se:2509毫秒,Xe:1325毫秒)

主题:雅虎!!我的网站是一个好网站|我的网站
“ >>>重新搜索(r'(\d+[\s\w]+\(\124;\(\ d+),s1).组(2) '12343' >>>重新搜索(r'(\d+[\s\w]+\(\124;\(\ d+),s2).组(1) '4001'
这一个一次匹配两种模式,并提取所需的数字:

tt = int(re.search(r"\?ent took (too long \()?(?P<num>\d+)",message).group('num'))
tt=int(重新搜索(r“\?ent花费了(太长的\()?(?P\d+),消息)。组('num'))

这一个一次匹配两种模式,并提取所需的数字:

tt = int(re.search(r"\?ent took (too long \()?(?P<num>\d+)",message).group('num'))
tt=int(重新搜索(r“\?ent花费了(太长的\()?(?P\d+),消息)。组('num'))

您查看了/usr/lib64/python2.7/re.py的第242行了吗?您查看了/usr/lib64/python2.7/re.py的第242行了吗?他可能想匹配URL中标记查询字符串开头的文本
。@liv2hak,您可以使用
r'\(\d+)“
签出编辑!@Kasra-我不知道我将获得哪一条消息。我需要一个同时匹配这两条消息的模式。@Kasra-如果我使用`tt=int(re.search(r)(\d+[\s\w]+(|)(\d+),message.group(1)))`我得到了
AttributeError:“unicode”对象没有属性“group”
@liv2hak,但我在回答中添加了它,它对我来说工作得很好!你确定你正确运行了吗?他可能想匹配URL中标记查询字符串开头的文本
。@liv2hak,你可以使用
r'\(\d+)“
签出编辑!@Kasra-我不知道我将获得哪一条消息。我需要一个同时匹配这两条消息的模式。@Kasra-如果我使用`tt=int(re.search(r)(\d+[\s\w]+(|)(\d+),message.group(1)))`我得到了
AttributeError:'unicode'对象没有属性“group”
@liv2hak,但正如我在回答中添加的那样,它对我来说运行良好!您确定您正确运行了它吗?