[英]re.search() logically or two patterns in re.search()
我有以下字符串。
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 too long (12343 ms Ne: 167 ms, Se: 2509 ms, Xe: 1325 ms)<br><br><br>Topic: Yahoo!! My website is a good website | Mywebsite<br>
我想從ent took 4001 ms
提取4001
OR 12343
以上, ent took 4001 ms
或ent took too long (12343 ms
並將其分配給一個變量
tt = int(re.search(r"\?ent\s*took\s*(\d+)",message).group(1))
此正則表達式確實匹配了第一部分,並確實返回了我4001。如何邏輯上或將表達式r"\\?ent\\s*\\took\\s*too\\s*long\\s*\\((\\d+)"
提取出來第二部分12343?
正則表達式開頭的問號沒有出現任何可以選擇的內容。 如果要在此處匹配文字問號,請寫\\?
:
x = int(re.findall(r"\?ent\s*took\s*([^m]*)",message)[0])
首先你需要逃脫?
在您的模式的前面,因為?
mark是一個正則表達式字符,並且使字符串成為可選字符串,並且必須在字符串之前! 所以如果你想數學?
您需要使用\\?
另外,作為一種更有效的方法,您可以在模式中使用re.search
和\\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="Page load for http://xxxx?roxy=www.yahoo.com&sendto=https://mywebsite?ent took too long (12343 ms Ne: 167 ms, Se: 2509 ms, Xe: 1325 ms)<br><br><br>Topic: Yahoo!! My website is a good website | Mywebsite<br>"
>>> s2="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>"
>>> re.search(r'(\d+)[\s\w]+\(|\((\d+)',s1).group(2)
'12343'
>>> re.search(r'(\d+)[\s\w]+\(|\((\d+)',s2).group(1)
'4001'
這一次匹配了兩種模式,並提取了所需的數字:
tt = int(re.search(r"\?ent took (too long \()?(?P<num>\d+)",message).group('num'))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.