I am trying to split a target sentence into composite pieces for a later function using re.split() and the regex
(@?\w+)(\W+)
Ideally, this would split words and non-word characters in a generated list, preserving both as separate list items, with the exception of the "@" symbol which could precede a word. If there is an @ symbol before a word, I want to keep it as a cohesive item in the split. My example is below.
My test sentence is as follows:
this is a test of proper nouns @Ryan
So the line of code is:
re.split(r'(@?\w+)(\W+)', "this is a test of proper nouns @Ryan")
The list that I want to generate would include "@Ryan" as a single item but, instead, it looks like this
['', 'this', ' ', '', 'is', ' ', '', 'a', ' ', '', 'test', ' ', '', 'of', ' ', '', 'proper', ' ', '', 'nouns', ' @', 'Ryan']
Since the first container has the @ symbol, I would have thought that it would be evaluated first but that is apparently not the case. I have tried using lookaheads or removing @ from the \W+ container to no avail.
