Python拆分一个带有多个分隔符的字符串并找到使用的分隔符

时间:2022-11-16 22:07:43

How to split string with multiple delimiters and find out which delimiter was used to split the string with a maxsplit of 1.

如何使用多个分隔符拆分字符串,并找出使用哪个分隔符来分割maxsplit为1的字符串。

import re

string ="someText:someValue~"
re.split(":|~",string,1)

returns ['someText', 'someValue~']. In this case ":" was the delimiter to split the string.

返回['someText','someValue~']。在这种情况下,“:”是分割字符串的分隔符。

If string is string ="someText~someValue:", then "~" will be delimiter to split the string

如果string是string =“someText~someValue:”,则“〜”将分隔符以分割字符串

Is there a way to find out which delimitor was used and store that in a variable.

有没有办法找出使用哪个分隔符并将其存储在变量中。

PS: someText and someValue may contain special chars, that are not used in split. Eg: some-Text, some_Text, some$Text

PS:someText和someValue可能包含特殊的字符,不用于拆分。例如:some-Text,some_Text,some $ Text

3 个解决方案

#1


4  

string ="someText:someValue~"
print re.split("(:|~)",string,1)

If you put in group,it will appear in the list returned.You can find it from 1 index of list.

如果你放入组,它将出现在返回的列表中。你可以从列表的1个索引中找到它。

#2


1  

You may use re.findall.

你可以使用re.findall。

>>> string ="someText:someValue~"
>>> re.findall(r'^([^:~]*)([:~])([^:~].*)', string)
[('someText', ':', 'someValue~')]

#3


0  

You can use re.findall to find the none words delimiters using look around:

您可以使用re.findall使用环顾四周找到无单词分隔符:

>>> string ="someText:someValue~andthsi#istest@"
>>> re.findall('(?<=\w)(\W)(?=\w)',string)
[':', '~', '#']

#1


4  

string ="someText:someValue~"
print re.split("(:|~)",string,1)

If you put in group,it will appear in the list returned.You can find it from 1 index of list.

如果你放入组,它将出现在返回的列表中。你可以从列表的1个索引中找到它。

#2


1  

You may use re.findall.

你可以使用re.findall。

>>> string ="someText:someValue~"
>>> re.findall(r'^([^:~]*)([:~])([^:~].*)', string)
[('someText', ':', 'someValue~')]

#3


0  

You can use re.findall to find the none words delimiters using look around:

您可以使用re.findall使用环顾四周找到无单词分隔符:

>>> string ="someText:someValue~andthsi#istest@"
>>> re.findall('(?<=\w)(\W)(?=\w)',string)
[':', '~', '#']