Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.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_String - Fatal编程技术网

如何在python中使用正则表达式捕获两个单词之间的字符?

如何在python中使用正则表达式捕获两个单词之间的字符?,python,regex,string,Python,Regex,String,如何在python中使用正则表达式捕获两个单词之间的字符串?另外,只返回两个单词之间的第一个集合 例如,我想捕捉“Hi”和“Best”之间的所有单词 original_string=“text text words你好,我需要使用正则表达式的帮助,Best Chris more words你好这不是我想要的,Best” 所需的\u string=“在这里,我需要使用正则表达式的帮助,” 我知道您使用的是:(?s)(?您需要调用该方法来返回匹配的字符串: >>> import r

如何在python中使用正则表达式捕获两个单词之间的字符串?另外,只返回两个单词之间的第一个集合

例如,我想捕捉“Hi”和“Best”之间的所有单词

original_string=“text text words你好,我需要使用正则表达式的帮助,Best Chris more words你好这不是我想要的,Best”

所需的\u string=“在这里,我需要使用正则表达式的帮助,”

我知道您使用的是:(?s)(?您需要调用该方法来返回匹配的字符串:

>>> import re
>>> original_string = "text text words Hi there, I need help using regular expressions ,Best Chris more words Hi This is not what I want ,Best"
>>> desired_string = re.search("(?s)(?<=Hi).+?(?=Best)", original_string).group()
>>> desired_string
' there, I need help using regular expressions ,'
>>>
>>重新导入
>>>original_string=“text text words你好,我需要使用正则表达式的帮助,Best Chris more words你好这不是我想要的,Best”
>>>所需字符串=重新搜索((?s)(?)?
>>> import re
>>> original_string = "text text words Hi there, I need help using regular expressions ,Best Chris more words Hi This is not what I want ,Best"
>>> desired_string = re.search("(?s)(?<=Hi).+?(?=Best)", original_string).group()
>>> desired_string
' there, I need help using regular expressions ,'
>>>