0

I have a field that can filter over some db columns. But I'm having trouble splitting the search string as I need:

I have this example:

import re
search = '   test "no splits234" this-splits   this_not_splits  asdf123  '
re.split(r'[\s]*[\W][\s]*', search.strip())
['test', 'no', 'splits234', 'this', 'splits', 'this_not_splits', 'asdf123']

Need this output:

['test', 'no splits234', 'this', 'splits', 'this_not_splits', 'asdf', '123']

Do not splits what is in quotes and splits text from numbers. How can I do this?

1
  • Don't name strings str. It overrides a builtin name Commented Jun 15, 2018 at 16:06

1 Answer 1

2

You can use findall with this regex:

>>> search = '   test "no splits234" this-splits   this_not_splits  asdf123  '
>>> print re.findall(r'"[^"\\]*(?:\\.[^"\\]*)*"|[^\s-]+', search)
['test', '"no splits234"', 'this', 'splits', 'this_not_splits', 'asdf123']

RegEx Details:

  • Expression "[^"\\]*(?:\\.[^"\\]*)*" matches string enclosed by double quotes ignoring all escaped quotes.
  • If there is no quoted string then we simply match 1+ non-space, non-hyphen characters using [^\s-]+

If you want to avoid capturing quotes then use:

>>> print re.findall(r'(?<=")[^"\\]*(?:\\.[^"\\]*)*(?=")|[^\s"-]+', search)
['test', 'no splits234', 'this', 'splits', 'this_not_splits', 'asdf123']

UPDATE:

OP has also shown last asdf123 splitting into asdf and 123. For that following regex may work:

>>> print re.findall(r'(?<=")[^"\\]*(?:\\.[^"\\]*)*(?=")|\b[a-zA-Z]+(?=\d)|(?<=[a-zA-Z])\d+|[^\s"-]+', search)
['test', 'no splits234', 'this', 'splits', 'this_not_splits', 'asdf', '123']
Sign up to request clarification or add additional context in comments.

3 Comments

Almost what I need ... But 'this-splits' should be divided.
Thanks! Have some way to split 'asdf' from '123' too?
asdf123 is strange as there is no delimiter. You didn't clarify this in question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.