Python重新提取模式后的第一个单词

2024-04-26 23:31:41 发布

您现在位置:Python中文网/ 问答频道 /正文

试图在大量文本blob中找到一个模式“todestinationlocation”。 使用以下方法:

pattern = re.compile("to (.*)")
string = #text blob
pattern.search(string)
# When I see the output
>>> _.group(1)
DestinationLocation blah blah blah ...

如何从捕获的文本中只提取“destinationLocation”?在


Tags: to方法text文本researchstring模式
1条回答
网友
1楼 · 发布于 2024-04-26 23:31:41

您想找到一个只由字母字符组成的单词,长度为1或更多,而不包含以下空格,因此请使用\w+(长度为1+的单个alpha):

pattern = re.compile("to (\w+)")

现在,如果单词实际上是words(比如to the mall),那么您将需要一些字符来指示单词序列的结尾,比如to the mall.(点限制器),以及带有可用字符集的include空格,以点结尾:

^{pr2}$

相关问题 更多 >