[英]How to extract a specific string in Python
I am trying to execute this curl
command using python
.我正在尝试使用
python
执行此curl
命令。 It retrieves an output like below.它检索 output,如下所示。
* Rebuilt URL to: <dns>
* Trying <ip>...
* TCP_NODELAY set
* Connected to escortpersonaladz.com (<ip>) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* successfully set certificate verify locations:
* CAfile: /etc/ssl/certs/ca-certificates.crt
CApath: /etc/ssl/certs
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
* TLSv1.3 (IN), TLS handshake, Server hello (2):
* TLSv1.3 (IN), TLS Unknown, Certificate Status (22):
* SSL connection using TLSv1.3 / TLS_AES_256_GCM_SHA384
* ALPN, server accepted to use h2
* Server certificate:
* subject: CN=webdisk.escortpersonaladz.com
* start date: May 3 02:00:53 2020 GMT
* expire date: Aug 1 02:00:53 2020 GMT
* subjectAltName: host "escortpersonaladz.com" matched cert's "escortpersonaladz.com"
How can I extract that particular line * expire date: Aug 1 02:00:53 2020 GMT
from the above output if it is exist?如果存在,我如何从上述 output 中提取该特定行
* expire date: Aug 1 02:00:53 2020 GMT
?
import re
curl_output = '''
* Rebuilt URL to: <dns>
* Trying <ip>...
* TCP_NODELAY set
* Connected to escortpersonaladz.com (<ip>) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* successfully set certificate verify locations:
* CAfile: /etc/ssl/certs/ca-certificates.crt
CApath: /etc/ssl/certs
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
* TLSv1.3 (IN), TLS handshake, Server hello (2):
* TLSv1.3 (IN), TLS Unknown, Certificate Status (22):
* SSL connection using TLSv1.3 / TLS_AES_256_GCM_SHA384
* ALPN, server accepted to use h2
* Server certificate:
* subject: CN=webdisk.escortpersonaladz.com
* start date: May 3 02:00:53 2020 GMT
* expire date: Aug 1 02:00:53 2020 GMT
* subjectAltName: host "escortpersonaladz.com" matched cert's "escortpersonaladz.com"
'''
match = re.search(r"(\*\s*expire date(.+?))\s*\*", curl_output)
if match:
desired = match.group(1)
print(desired)
#* expire date: Aug 1 02:00:53 2020 GMT
else:
print("Not found")
The regex matches the string you are looking for by looking between two *
s that contain the expire date
.正则表达式通过在两个包含
expire date
的*
之间查找来匹配您要查找的字符串。 It also accounts for possible spaces before and after.它还考虑了前后可能的空间。 If a match is found, desired string lies in the first group of the match object.
如果找到匹配项,则所需字符串位于匹配项 object 的第一组中。 In case it is not found,
re.search
will return None
, so we check and act accordingly.如果找不到,
re.search
将返回None
,因此我们检查并采取相应措施。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.