Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.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,我有一根像下面一样的绳子 400 get INTERNET/RESULTS http?=likelyanswer, (localhost) 900ms 尝试仅返回900ms,但我似乎在这方面遇到了问题,尝试了python正则表达式仍然没有得到令人满意的结果。您的字符串总是那么可预测吗?为什么要用正则表达式呢 In : x = '400 get INTERNET/RESULTS http?=likelyanswer, (localhost) 900ms' In : x.split(' ')[-1

我有一根像下面一样的绳子

400 get INTERNET/RESULTS http?=likelyanswer, (localhost) 900ms

尝试仅返回900ms,但我似乎在这方面遇到了问题,尝试了python正则表达式仍然没有得到令人满意的结果。

您的字符串总是那么可预测吗?为什么要用正则表达式呢

In : x = '400 get INTERNET/RESULTS http?=likelyanswer, (localhost) 900ms'
In : x.split(' ')[-1]
Out: '900ms'

如果您只需要最后一个空格分隔的项,那么只需使用
rsplit()


这将返回最后一个空格分隔的项。

“尝试过的python正则表达式”显示您的尝试。顺便说一句,看起来您不必使用正则表达式:
s.rsplit()[-1]
将执行此操作,其中
s
是您的字符串。结果并不总是可预测的,但格式接近于此。这就是为什么我打算在拼接之前搜索整个文件以查找匹配项。我想我的问题写得不好
result_string = "400 get INTERNET/RESULTS http?=likelyanswer, (localhost) 900ms"
ms_time = result_string.rsplit(" ",1)[1]