从给定字符串中查找子字符串旁边的子字符串

  • 本文关键字:字符串 查找 python
  • 更新时间 :
  • 英文 :


如何从给定字符串中找到所提到的子字符串旁边的子字符串。

例如:

string = "无法增加索引orcl_index的空间。索引orcl_index的空间应该增加250mb">

指示子字符串这里是"index",所需输出将是"orcl_index"

我尝试了下面的代码,但不确定如何继续

my_string = "unable to increase the space of the index orcl_index. The space for the index orcl_index should be increased by 250mb"
print(my_string.split("index",1)[1])
a= my_string.split("index",1)[1]
b= a.strip()
print(b)
Output:" orcl_index should be increased by 250mb"
Required output: "orcl_index"

如果您愿意使用正则表达式,有一个简单的方法:

import re
inp = "unable to increase the space of the index orcl_index. The space for the index orcl_index should be increased by 250mb"
val = re.search(r'bindex (w+)', inp).group(1)
print(val)  # orcl_index

这里使用的正则表达式模式是bindex (w+),它表示匹配:

  • bindex字"索引">
  • 单间距
  • (w+)匹配第一个捕获组中的下一个单词和捕获

最新更新