Reputation: 779
I am trying to split a line of text that looks something like this
host=randomhostid123 moreinfo id=otherstuffhere version="morestuff" type=TYPEA::TYPEB
i am tying to use split to parse it into
host=randomhostid123 moreinfo
id=otherstuffhere
version="morestuff"
type=TYPEA::TYPEB
to do this I was using
str.split('?[a-zA-Z]*=')
but all this is producing is the original string all over again I think the regex looks ok, but I am new to python regex
Upvotes: 0
Views: 60
Reputation: 44269
You are using str.split()
. What you want is re.split
:
re.split(r'\s+(?=[a-zA-Z]+=)', str)
This will split on spaces which are followed by words which in turn are immediately followed by =
. Note, that you have to put everything but the spaces in a lookahead, so that it is not swallowed by the split operation.
Upvotes: 3
Reputation: 33019
The ?
in your regex is being parsed literally, and since there's no ?
in your string it doesn't split. I think you may have been going for a regex lookahead match, but string.split
doesn't support regexes.
Ah—too long looking at the documentation! m.buettner summarized things very nicely.
Upvotes: 0