I know this is probably really easy question, but i'm struggling to split a string in python. My regex has group separators like this:
myRegex = "(\W+)"
testString = "This is my test string, hopefully I can get the word i need"
testAgain = re.split("(\W+)", testString)
['This', ' ', 'is', ' ', 'my', ' ', 'test', ' ', 'string', ', ', 'hopefully', ' ', 'I', ' ', 'can', ' ', 'get', ' ', 'the', ' ', 'word', ' ', 'i', ' ', 'need']
As described in this answer, How to split but ignore separators in quoted strings, in python?, you can simply slice the array once it's split. It's easy to do so because you want every other member, starting with the first one (so 1,3,5,7)
You can use the [start:end:step] notation as described below:
testString = "This is my test string, hopefully I can get the word i need" testAgain = re.split("(\W+)", testString) testAgain = testAgain[0::2]
Also, I must point out that
\W matches any non-word characters, including punctuation. If you want to keep your punctuation, you'll need to change your regex.