在Python中使用多个分隔符分割字符串[重复]

本文翻译自:Split string with multiple delimiters in Python [duplicate]

This question already has an answer here: 这个问题已经在这里有了答案:

  • Split Strings into words with multiple word boundary delimiters 31 answers 将字符串拆分为具有多个单词边界定界符的单词 31个答案

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 没有尾随空格的单个逗号应保持不变

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]') 

#1楼

参考:https://stackoom.com/question/KyN3/在Python中使用多个分隔符分割字符串-重复


#2楼

Here's a safe way for any iterable of delimiters, using regular expressions: 这是使用正则表达式进行定界符迭代的一种安全方法:

>>> import re
>>> delimiters = "a", "...", "(c)"
>>> example = "stackoverflow (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. re.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


#3楼

In response to Jonathan's answer above, this only seems to work for certain delimiters. 为了回应Jonathan的上述回答,这似乎仅对某些定界符有效。 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']

#4楼

str.split(', ')一个str.split(', ') str.replace('; ', ', ')然后做一个str.split(', ')


#5楼

This is how the regex look like: 正则表达式如下所示:

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)

#6楼

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']

你可能感兴趣的:(python,string,split,delimiter)