如何在特定子字符串后获取字符串.
例如,我想以后得到的字符串"world"
中my_string="hello python world , i'm a beginner "
Jor*_*ley 334
最简单的方法可能只是拆分你的目标词
my_string="hello python world , i'm a beginner "
print my_string.split("world",1)[1]
Run Code Online (Sandbox Code Playgroud)
split将单词(或字符)拆分,并可选择限制拆分数.
在此示例中拆分"世界"并将其限制为仅一个拆分.
ars*_*jii 60
s1 = "hello python world , i'm a beginner "
s2 = "world"
print s1[s1.index(s2) + len(s2):]
Run Code Online (Sandbox Code Playgroud)
如果你要处理的情况下s2
是不存在的s1
,然后使用s1.find(s2)
而不是index
.如果该调用的返回值是-1
,s2
则不在s1
.
shx*_*hx2 47
我很惊讶没有人提到过partition
.
def substring_after(s, delim):
return s.partition(delim)[2]
Run Code Online (Sandbox Code Playgroud)
恕我直言,这个解决方案比@ arshajii更具可读性.除此之外,我认为@ arshajii是最快的 - 它不会创建任何不必要的副本/子串.
Tad*_*dgh 17
如果你想用正则表达式做这个,你可以简单地使用一个非捕获组来获取"世界"这个词然后抓住所有内容,就像这样
(?:world).*
Run Code Online (Sandbox Code Playgroud)
此处测试示例字符串
您要使用str.partition()
:
>>> my_string.partition("world")[2]
" , i'm a beginner "
Run Code Online (Sandbox Code Playgroud)
因为此选项比其他选项要快。
请注意,如果缺少分隔符,则会生成一个空字符串:
>>> my_string.partition("Monty")[2] # delimiter missing
''
Run Code Online (Sandbox Code Playgroud)
如果要使用原始字符串,请测试从中返回的第二个值str.partition()
是否非空:
prefix, success, result = my_string.partition(delimiter)
if not success: result = prefix
Run Code Online (Sandbox Code Playgroud)
您也可以使用str.split()
1:
>>> my_string.split("world", 1)[-1]
" , i'm a beginner "
>>> my_string.split("Monty", 1)[-1] # delimiter missing
"hello python world , i'm a beginner "
Run Code Online (Sandbox Code Playgroud)
但是,此选项较慢。在最佳情况下,与相比,str.partition()
轻松快约15%str.split()
:
>>> my_string.partition("world")[2]
" , i'm a beginner "
Run Code Online (Sandbox Code Playgroud)
这显示了每次执行的时间,并带有输入,此处缺少分隔符(最坏情况),放在最前面(最佳情况)或位于下半部,上半部或最后位置。最快的时间标有[...]
,<...>
而最坏的则标有。
上表是针对以下所有三种选择的综合时间试用得出的。我在具有2.9 GHz Intel Core i7和16 GB内存的2017年型号15“ Macbook Pro上的Python 3.7.4上运行了测试。
该脚本会生成带有或不带有随机选择的定界符的随机句子,如果存在,则在生成的句子中的不同位置,以重复的随机顺序运行测试(产生最合理的结果,说明测试期间发生的随机OS事件),然后打印结果表:
>>> my_string.partition("Monty")[2] # delimiter missing
''
Run Code Online (Sandbox Code Playgroud)
这是一个老问题,但我面临着一个非常相同的情况,我需要使用单词“low”作为分词来分割字符串,对我来说问题是我在同一个字符串中包含下面和更低的单词。
我用 re 模块这样解决了这个问题
import re
string = '...below...as higher prices mean lower demand to be expected. Generally, a high reading is seen as negative (or bearish), while a low reading is seen as positive (or bullish) for the Korean Won.'
# use re.split with regex to match the exact word
stringafterword = re.split('\\blow\\b',string)[-1]
print(stringafterword)
# ' reading is seen as positive (or bullish) for the Korean Won.'
# the generic code is:
re.split('\\bTHE_WORD_YOU_WANT\\b',string)[-1]
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助别人!
小智 6
您可以使用名为substring
. 只需使用命令安装pip install substring
。您只需提及开始和结束字符/索引即可获得子字符串。
例如:
import substring
s = substring.substringByChar("abcdefghijklmnop", startChar="d", endChar="n")
print(s)
Run Code Online (Sandbox Code Playgroud)
输出:
# s = defghijklmn
Run Code Online (Sandbox Code Playgroud)
试试这个通用方法:
import re
my_string="hello python world , i'm a beginner "
p = re.compile("world(.*)")
print (p.findall(my_string))
#[" , i'm a beginner "]
Run Code Online (Sandbox Code Playgroud)
在 Python 3.9 中,removeprefix
添加了一个新方法:
>>> 'TestHook'.removeprefix('Test')
'Hook'
>>> 'BaseTestCase'.removeprefix('Test')
'BaseTestCase'
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
364693 次 |
最近记录: |