0

I have a big block of text:

SELECT TOP 10 * FROM APPLES
GO
SELECT TOP 10 * FROM PEARS TREE
GO
SELECT TOP 10 * FROM FRUITS
...

And I'm simply trying to split this text into a list of individual strings based around GO, this works:

commandlist = textblock.split("GO")

but.. what I'd like to do is use something like:

commandlist = textblock.split(r"\bGO\b")

because I'm expecting some text to look like:

SELECT TOP 10 * FROM GOPATRIOTS
GO
SELECT TOP 10 * FROM PEARS LETITGO
GO SELECT TOP 10 * FROM FRUITS
...

but it seems that I can't just shove a regex into split? Or can I and I'm just missing the way to do so?

1
  • 1
    Use re.split. So basic, I can't find a dupe. Commented Jun 9, 2017 at 19:15

1 Answer 1

2

You need to use re.split, not a string split:

import re
commandlist = re.split(r"\bGO\b", textblock)

Or, since you need to split with lines equal to GO:

commandlist = re.split(r"(?m)^GO$", textblock)
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect, that's exactly what I wanted. Code works great. I'll accept once it let's me. I love it when I can simply copy/paste the answer over my old line and it just works.

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.