简体   繁体   English

如何使用split或regex从python中的字符串获取子字符串

[英]How to get sub string from a string in python using split or regex

I have a str in python like below. 我在python中有一个str ,如下所示。 I want extract a substring from it. 我想从中提取一个子串。

table='abc_test_01'

number=table.split("_")[1]

I am getting test as a result. 结果我正在test

What I want is everything after the first _ . 我想要的是第一个_之后的所有内容。

The result I want is test_01 how can I achieve that. 我想要的结果是test_01如何实现。

You could do it like: 您可以这样做:

import re
string = "abc_test_01"

rx = re.compile(r'[^_]*_(.+)')
match = rx.match(string).group(1)
print(match)

Or with normal string functions: 或使用普通的字符串函数:

string = "abc_test_01"

match = '_'.join(string.split('_')[1:])
print(match)

To get the substring (all characters after the first occurrence of underscore): 要获取子字符串(第一次出现下划线之后的所有字符):

number = table[table.index('_')+1:]
# Output: test_01

You can try this: 您可以尝试以下方法:

Edit: Thanks to @valtah 's comment: 编辑:感谢@valtah的评论:

table = 'abc_test_01'
#final = "_".join(table.split("_")[1:])
final = table.split("_", 1)[1]
print final 

Output: 输出:

'test_01'

Also the answer of @valtah in the comment is correct: 另外,@ valtah在评论中的答案是正确的:

final = table.partition("_")[2]
print final 

Will output the same result 将输出相同的结果

Nobody mentions that the split() function can have an maxsplit argument: 没有人提到split()函数可以具有maxsplit参数:

str.split(sep=None, maxsplit=-1) str.split(sep =无,maxsplit = -1)

return a list of the words in the string, using sep as the delimiter string. 使用sep作为分隔符字符串,返回字符串中单词的列表。 If maxsplit is given, at most maxsplit splits are done (thus, the list will have at most maxsplit +1 elements). 如果指定了maxsplit,则最多完成maxsplit分割(因此,列表最多包含maxsplit +1个元素)。

So the solution is only: 因此,解决方案仅是:

table.split('_', 1)[1]

Here is the code as already given by many of them 这是许多人已经给出的代码

table='abc_test_01'
number=table.split("_",1)[1]

But the above one may fail in situations when the occurrence is not in the string, then you'll get IndexError: list index out of range 但是在出现不在字符串中的情况下,上述方法可能会失败,那么您将得到IndexError: list index out of range

For eg. 例如。

table='abctest01'
number=table.split("_",1)[1]

The above one will raise IndexError , as the occurrence is not in the string 上面的代码将引发IndexError ,因为出现的内容不在字符串中

So the more accurate code for handling this is 因此,用于处理此问题的更准确的代码是

table.split("_",1)[-1]

Therefore -1 will not get any harm because the number of occurrences is already set to one. 因此,-1不会受到任何损害,因为出现的次数已经设置为1。

Hope it helps :) 希望能帮助到你 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM