Python delimited string on multiple metrics without regex

I have a line in which I need to split into multiple characters without using regular expressions. For example, I will need the following:

>>>string="hello there[my]friend"
>>>string.split(' []')
['hello','there','my','friend']

is there anything in python?

+3
source share
3 answers

If you need multiple delimiters, re.splitthis is the way to go.

Without using a regular expression, this is not possible if you have not written a custom function for it.

Here's a function - it may or may not do what you want (consecutive delimiters call empty elements):

>>> 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']
+6
source

Solution without regex:

from itertools import groupby
sep = ' []'
s = 'hello there[my]friend'
print [''.join(g) for k, g in groupby(s, sep.__contains__) if not k]

fooobar.com/questions/16012/...

0

re.split .

>>> string="hello there[my]friend"
>>> import re
>>> re.split('[] []', string)
['hello', 'there', 'my', 'friend']

In regex, [...]defines a character class. Any characters inside the brackets will match. The way I put the brackets avoids the need to avoid them, but the template also works [\[\] ].

>>> re.split('[\[\] ]', string)
['hello', 'there', 'my', 'friend']

The flag re.DEBUGfor re.compile is also useful as it prints to fit the pattern:

>>> re.compile('[] []', re.DEBUG)
in 
  literal 93
  literal 32
  literal 91
<_sre.SRE_Pattern object at 0x16b0850>

(where 32, 91, 93, - ascii values assigned , [, ])

-1
source

All Articles