[英]python split string on multiple delimeters without regex
我有一个字符串,我需要在不使用正则表达式的情况下拆分多个字符。 例如,我需要如下内容:
>>>string="hello there[my]friend"
>>>string.split(' []')
['hello','there','my','friend']
python里面有这样的吗?
如果您需要多个分隔符, re.split
是要走的路。
不使用正则表达式,除非您为它编写自定义函数,否则这是不可能的。
这是一个这样的函数 - 它可能会或可能不会做你想要的(连续的分隔符导致空元素):
>>> def multisplit(s, delims):
... pos = 0
... for i, c in enumerate(s):
... if c in delims:
... yield s[pos:i]
... pos = i + 1
... yield s[pos:]
...
>>> list(multisplit('hello there[my]friend', ' []'))
['hello', 'there', 'my', 'friend']
没有正则表达式的解决方案:
from itertools import groupby
sep = ' []'
s = 'hello there[my]friend'
print [''.join(g) for k, g in groupby(s, sep.__contains__) if not k]
我刚刚在这里发布了一个解释https://stackoverflow.com/a/19211729/2468006
不使用正则表达式的递归解决方案。 与其他答案相比,仅使用基本 python。
def split_on_multiple_chars(string_to_split, set_of_chars_as_string):
# Recursive splitting
# Returns a list of strings
s = string_to_split
chars = set_of_chars_as_string
# If no more characters to split on, return input
if len(chars) == 0:
return([s])
# Split on the first of the delimiter characters
ss = s.split(chars[0])
# Recursive call without the first splitting character
bb = []
for e in ss:
aa = split_on_multiple_chars(e, chars[1:])
bb.extend(aa)
return(bb)
与 python 的常规string.split(...)
非常相似,但接受几个分隔符。
使用示例:
print(split_on_multiple_chars('my"example_string.with:funny?delimiters', '_.:;'))
输出:
['my"example', 'string', 'with', 'funny?delimiters']
如果您不担心长字符串,可以使用 string.replace() 强制所有定界符相同。 以下将字符串拆分为-
和,
x.replace('-', ',').split(',')
如果您有许多定界符,您可以执行以下操作:
def split(x, delimiters):
for d in delimiters:
x = x.replace(d, delimiters[0])
return x.split(delimiters[0])
re.split
是正确的工具。
>>> string="hello there[my]friend"
>>> import re
>>> re.split('[] []', string)
['hello', 'there', 'my', 'friend']
在正则表达式中, [...]
定义了一个字符类。 括号内的任何字符都将匹配。 我分隔括号的方式避免了需要对它们进行转义,但模式[\\[\\] ]
也有效。
>>> re.split('[\[\] ]', string)
['hello', 'there', 'my', 'friend']
re.compile 的re.DEBUG
标志也很有用,因为它会打印出匹配的模式:
>>> re.compile('[] []', re.DEBUG)
in
literal 93
literal 32
literal 91
<_sre.SRE_Pattern object at 0x16b0850>
(其中 32、91、93 是分配给的 ascii 值 ,
[
, ]
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.