1

I want to print all names with the example below. Names are random every time.

txt = "[something name=\"Paul\" other=\"1/1/1\"][something name=\"James\" other=\"4/3/5\"][something name=\"Victor\" other=\"7/2/6\"][something name=\"Jane\" other=\"4/3/6\"]" 

I know how to print first of this names:

print str(txt[txt.index('[something name=\"')+17:txt.index(' other')-1]) 

but how can I print all? I need to print all names in new line:

Paul James Victor Jane 

2 Answers 2

5

Looks like you can use regex here:

import re txt = "[something name=\"Paul\" other=\"1/1/1\"][something name=\"James\" other=\"4/3/5\"][something name=\"Victor\" other=\"7/2/6\"][something name=\"Jane\" other=\"4/3/6\"]" for name in re.findall('name\=\\"(.*?)\\\"', txt): print name 

Prints:

Paul James Victor Jane 
Sign up to request clarification or add additional context in comments.

Comments

1

Another approach would be to split the string as follows:

txt = "[something name=\"Paul\" other=\"1/1/1\"][something name=\"James\" other=\"4/3/5\"][something name=\"Victor\" other=\"7/2/6\"][something name=\"Jane\" other=\"4/3/6\"]" for x in txt.split(']'): if len(x): print x.split('"', 2)[1] 

Giving:

Paul James Victor Jane 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.