在特定的子字符串之后获取一个字符串。

时间:2022-09-13 09:27:24

how can i get a string after a specific substring .

如何在特定子字符串之后获得字符串。

For example I want to get the string after "world" in my_string="hello python world , i'm a beginner "

例如,我想在my_string="hello python世界"中获得"world"后的字符串,我是初学者。

6 个解决方案

#1


214  

The easiest way is probably just to split on your target word

最简单的方法可能就是将你的目标词拆分

my_string="hello python world , i'm a beginner "
print my_string.split("world",1)[1] 

split takes the word(or character) to split on and optionally a limit to the number of splits.

split将单词(或字符)分隔开,并可以选择对分割的数量进行限制。

In this example split on "world" and limit it to only one split.

在这个例子中,在“世界”上分割,并将它限制为一个分割。

#2


33  

s1 = "hello python world , i'm a beginner "
s2 = "world"

print s1[s1.index(s2) + len(s2):]

If you want to deal with the case where s2 is not present in s1, then use s1.find(s2) as opposed to index. If the return value of that call is -1, then s2 is not in s1.

如果您想处理s2在s1中不存在的情况,那么请使用s1.find(s2),而不是索引。如果调用的返回值是-1,则s2不在s1中。

#3


30  

I'm surprised nobody mentioned partition.

我很惊讶没有人提到分区。

def substring_after(s, delim):
    return s.partition(delim)[2]

IMHO, this solution is more readable than @arshajii's. Other than that, I think @arshajii's is the best for being the fastest -- it does not create any unnecessary copies/substrings.

IMHO,这个解决方案比@arshajii的更具可读性。除此之外,我认为@arshajii是最快的——它不会创建任何不必要的拷贝/子字符串。

#4


12  

If you want to do this using regex, you could simply use a non-capturing group, to get the word "world" and then grab everything after, like so

如果你想用正则表达式来做这个,你可以简单地使用一个非捕获组,来得到单词“world”,然后再抓取所有东西,就像这样。

(?:world).*

The example string is tested here

这里测试示例字符串

#5


2  

It's an old question but i faced a very same scenario, i need to split a string using as demiliter the word "low" the problem for me was that i have in the same string the word below and lower.

这是一个老问题,但我遇到了一个非常相同的情况,我需要用"low"这个词来分割一个字符串对我来说,问题是我有一个相同的字符串下面的和下面的。

I solved it using the re module this way

我用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

使用re.split与regex进行匹配

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]

Hope this can help someone!

希望这能对别人有所帮助!

#6


0  

You can use this package called "substring". Just type "pip install substring". You can get the substring by just mentioning the start and end characters/indices.

您可以使用这个名为“substring”的包。只需输入“pip安装子字符串”。只要提到开始和结束字符/索引,就可以获得子字符串。

For example:

例如:

import substring

s = substring.substringByChar("abcdefghijklmnop", startChar="d", endChar="n")

print(s)

Output:

输出:

s = defghijklmn

#1


214  

The easiest way is probably just to split on your target word

最简单的方法可能就是将你的目标词拆分

my_string="hello python world , i'm a beginner "
print my_string.split("world",1)[1] 

split takes the word(or character) to split on and optionally a limit to the number of splits.

split将单词(或字符)分隔开,并可以选择对分割的数量进行限制。

In this example split on "world" and limit it to only one split.

在这个例子中,在“世界”上分割,并将它限制为一个分割。

#2


33  

s1 = "hello python world , i'm a beginner "
s2 = "world"

print s1[s1.index(s2) + len(s2):]

If you want to deal with the case where s2 is not present in s1, then use s1.find(s2) as opposed to index. If the return value of that call is -1, then s2 is not in s1.

如果您想处理s2在s1中不存在的情况,那么请使用s1.find(s2),而不是索引。如果调用的返回值是-1,则s2不在s1中。

#3


30  

I'm surprised nobody mentioned partition.

我很惊讶没有人提到分区。

def substring_after(s, delim):
    return s.partition(delim)[2]

IMHO, this solution is more readable than @arshajii's. Other than that, I think @arshajii's is the best for being the fastest -- it does not create any unnecessary copies/substrings.

IMHO,这个解决方案比@arshajii的更具可读性。除此之外,我认为@arshajii是最快的——它不会创建任何不必要的拷贝/子字符串。

#4


12  

If you want to do this using regex, you could simply use a non-capturing group, to get the word "world" and then grab everything after, like so

如果你想用正则表达式来做这个,你可以简单地使用一个非捕获组,来得到单词“world”,然后再抓取所有东西,就像这样。

(?:world).*

The example string is tested here

这里测试示例字符串

#5


2  

It's an old question but i faced a very same scenario, i need to split a string using as demiliter the word "low" the problem for me was that i have in the same string the word below and lower.

这是一个老问题,但我遇到了一个非常相同的情况,我需要用"low"这个词来分割一个字符串对我来说,问题是我有一个相同的字符串下面的和下面的。

I solved it using the re module this way

我用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

使用re.split与regex进行匹配

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]

Hope this can help someone!

希望这能对别人有所帮助!

#6


0  

You can use this package called "substring". Just type "pip install substring". You can get the substring by just mentioning the start and end characters/indices.

您可以使用这个名为“substring”的包。只需输入“pip安装子字符串”。只要提到开始和结束字符/索引,就可以获得子字符串。

For example:

例如:

import substring

s = substring.substringByChar("abcdefghijklmnop", startChar="d", endChar="n")

print(s)

Output:

输出:

s = defghijklmn