Python:带有多个分隔符的分割字符串[重复]

时间:2022-04-08 21:37:20

This question already has an answer here:

这个问题已经有了答案:

I found some answers online, but I have no experience with regular expressions, which I believe is what is needed here.

我在网上找到了一些答案,但我没有正则表达式的经验,我认为这正是这里所需要的。

I have a string that needs to be split by either a ';' or ', ' That is, it has to be either a semicolon or a comma followed by a space. Individual commas without trailing spaces should be left untouched

我有一个字符串需要被a ';'或',也就是说,它必须是分号或逗号后面跟着空格。没有拖尾空格的逗号应该保持原样

Example string:

示例字符串:

"b-staged divinylsiloxane-bis-benzocyclobutene [124221-30-3], mesitylene [000108-67-8]; polymerized 1,2-dihydro-2,2,4- trimethyl quinoline [026780-96-1]"

should be split into a list containing the following:

应分为以下清单:

('b-staged divinylsiloxane-bis-benzocyclobutene [124221-30-3]' , 'mesitylene [000108-67-8]', 'polymerized 1,2-dihydro-2,2,4- trimethyl quinoline [026780-96-1]') 

5 个解决方案

#1


494  

Luckily, Python has this built-in :)

幸运的是,Python有这个内置的:)

import re
re.split('; |, ',str)

Update:
Following your comment:

更新:以下你的评论:

>>> a='Beautiful, is; better*than\nugly'
>>> import re
>>> re.split('; |, |\*|\n',a)
['Beautiful', 'is', 'better', 'than', 'ugly']

#2


115  

Do a str.replace('; ', ', ') and then a str.split(', ')

做一个str.replace(';“、”、“”,然后是“斯普利特”(“、”)

#3


67  

Here's a safe way for any iterable of delimiters, using regular expressions:

对于任何可迭代的分隔符,这里有一个安全的方法,使用正则表达式:

>>> import re
>>> delimiters = "a", "...", "(c)"
>>> example = "* (c) is awesome... isn't it?"
>>> regexPattern = '|'.join(map(re.escape, delimiters))
>>> regexPattern
'a|\\.\\.\\.|\\(c\\)'
>>> re.split(regexPattern, example)
['st', 'ckoverflow ', ' is ', 'wesome', " isn't it?"]

re.escape allows to build the pattern automatically and have the delimiters escaped nicely.

escape允许自动构建模式,并让分隔符正确地转义。

Here's this solution as a function for your copy-pasting pleasure:

这里是这个解决方案作为你复制粘贴乐趣的一个函数:

def split(delimiters, string, maxsplit=0):
    import re
    regexPattern = '|'.join(map(re.escape, delimiters))
    return re.split(regexPattern, string, maxsplit)

If you're going to split often using the same delimiters, compile your regular expression beforehand like described and use RegexObject.split.

如果您打算经常使用相同的分隔符进行分割,请按照所描述的那样预先编译正则表达式并使用RegexObject.split。

#4


41  

In response to Jonathan's answer above, this only seems to work for certain delimiters. For example:

对于乔纳森的回答,这似乎只适用于某些分隔符。例如:

>>> a='Beautiful, is; better*than\nugly'
>>> import re
>>> re.split('; |, |\*|\n',a)
['Beautiful', 'is', 'better', 'than', 'ugly']

>>> b='1999-05-03 10:37:00'
>>> re.split('- :', b)
['1999-05-03 10:37:00']

By putting the delimiters in square brackets it seems to work more effectively.

通过将分隔符放在方括号中,似乎可以更有效地工作。

>>> re.split('[- :]', b)
['1999', '05', '03', '10', '37', '00']

#5


21  

This is how the regex look like:

这就是regex的样子:

import re
# "semicolon or (a comma followed by a space)"
pattern = re.compile(r";|, ")

# "(semicolon or a comma) followed by a space"
pattern = re.compile(r"[;,] ")

print pattern.split(text)

#1


494  

Luckily, Python has this built-in :)

幸运的是,Python有这个内置的:)

import re
re.split('; |, ',str)

Update:
Following your comment:

更新:以下你的评论:

>>> a='Beautiful, is; better*than\nugly'
>>> import re
>>> re.split('; |, |\*|\n',a)
['Beautiful', 'is', 'better', 'than', 'ugly']

#2


115  

Do a str.replace('; ', ', ') and then a str.split(', ')

做一个str.replace(';“、”、“”,然后是“斯普利特”(“、”)

#3


67  

Here's a safe way for any iterable of delimiters, using regular expressions:

对于任何可迭代的分隔符,这里有一个安全的方法,使用正则表达式:

>>> import re
>>> delimiters = "a", "...", "(c)"
>>> example = "* (c) is awesome... isn't it?"
>>> regexPattern = '|'.join(map(re.escape, delimiters))
>>> regexPattern
'a|\\.\\.\\.|\\(c\\)'
>>> re.split(regexPattern, example)
['st', 'ckoverflow ', ' is ', 'wesome', " isn't it?"]

re.escape allows to build the pattern automatically and have the delimiters escaped nicely.

escape允许自动构建模式,并让分隔符正确地转义。

Here's this solution as a function for your copy-pasting pleasure:

这里是这个解决方案作为你复制粘贴乐趣的一个函数:

def split(delimiters, string, maxsplit=0):
    import re
    regexPattern = '|'.join(map(re.escape, delimiters))
    return re.split(regexPattern, string, maxsplit)

If you're going to split often using the same delimiters, compile your regular expression beforehand like described and use RegexObject.split.

如果您打算经常使用相同的分隔符进行分割,请按照所描述的那样预先编译正则表达式并使用RegexObject.split。

#4


41  

In response to Jonathan's answer above, this only seems to work for certain delimiters. For example:

对于乔纳森的回答,这似乎只适用于某些分隔符。例如:

>>> a='Beautiful, is; better*than\nugly'
>>> import re
>>> re.split('; |, |\*|\n',a)
['Beautiful', 'is', 'better', 'than', 'ugly']

>>> b='1999-05-03 10:37:00'
>>> re.split('- :', b)
['1999-05-03 10:37:00']

By putting the delimiters in square brackets it seems to work more effectively.

通过将分隔符放在方括号中,似乎可以更有效地工作。

>>> re.split('[- :]', b)
['1999', '05', '03', '10', '37', '00']

#5


21  

This is how the regex look like:

这就是regex的样子:

import re
# "semicolon or (a comma followed by a space)"
pattern = re.compile(r";|, ")

# "(semicolon or a comma) followed by a space"
pattern = re.compile(r"[;,] ")

print pattern.split(text)