3

In my case the delimiter string is ' ' (3 consecutive spaces, but the answer should work for any multi-character delimiter), and an edge case text to search in could be this:

'Coord="GLOB"AL   Axis=X   Type="Y   ZR"   Color="Gray Dark"   Alt="Q   Z"qz   Loc=End'

The solution should return the following strings:

Coord="GLOB"AL
Axis=X
Type="Y   ZR"
Color="Gray Dark"
Alt="Q   Z"qz
Loc=End

I've looked for regex solutions, evaluating also the inverse problem (match multi-character delimiter unless inside quotes), since the re.split command of Python 3.4.3 allows to easily split a text by a regex pattern, but I'm not sure there is a regex solution, therefore I'm open also to (efficient) non regex solutions.

I've seen some solution to the inverse problem using lookahead/lookbehind containing regex pattern, but they did not work because Python lookahead/lookbehind (unlike other languages engine) requires fixed-width pattern.

This question is not a duplicate of Regex matching spaces, but not in "strings" or similar other questions, because:

  1. matching a single space outside quotes is different from matching a multi-character delimiter (in my example the delimiter is 3 spaces, but the question is about any multi-character delimiter);
  2. Python regex engine is slightly different from C++ or other languages regex engines;
  3. matching a delimiter is side B of my question, the direct question is about splitting a string.
Community
  • 1
  • 1
mmj
  • 5,514
  • 2
  • 44
  • 51

1 Answers1

4
x='Coord="GLOB"AL   Axis=X   Type="Y   ZR"   Color="Gray Dark"   Alt="Q   Z"qz   Loc=End'
print re.split(r'\s+(?=(?:[^"]*"[^"]*")*[^"]*$)',x)

You need to use lookahead to see if the space it not in between ""

Output ['Coord="GLOB"AL', 'Axis=X', 'Type="Y ZR"', 'Color="Gray Dark"', 'Alt="Q Z"qz', 'Loc=End']

For a generalized version if you want to split on delimiters not present inside "" use

re.split(r'delimiter(?=(?:[^"]*"[^"]*")*[^"]*$)',x)
vks
  • 67,027
  • 10
  • 91
  • 124
  • Great answer thanks! I believe it will deserve many upvotes. I thought it would have raised the `look-behind requires fixed-width pattern` error, but it doesn't: so much the better. – mmj Aug 20 '15 at 21:30