0

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

2
  • split does not accept regexps... only plain strings Commented Jun 27, 2013 at 23:41
  • As a side note, it's a very bad idea to call a string str. That's the name of the built-in type, and the function you use to convert things to strings. For example, you won't be able to do str(2) to get '2' if you've replaced the function with a string. Commented Jun 28, 2013 at 0:06

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

He's using str.split, not string.split.
0

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.

Comments

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.