2

I have formatted lines of the text, i.e.

[[item1 *,* {_item2*} *;{item3*}* ;{item4*}*]]

where * means any text between the words and brackets. Is it possible to collect text from * to variables?

item1, after1, before2, item2, after2, item3, after3, item4, after4, afterall = re. ???
1
  • Please provide detailed specifications about your "formatted text", preferably some real world examples so we can try them out. Commented Jan 19, 2011 at 5:22

1 Answer 1

2

You should be able to do it with regular expressions.

http://docs.python.org/library/re.html

You can put parenthesis around parts of the expression you want to pull out later.

Are you trying to grab the * parts or the item parts? If you trying to grab the * parts it shouldn't be too hard.

import re

reg = r'\[\[item1 (.*),(.*) {_item2(.*)} (.*);{item3(.*)}(.*) ;{item4(.*)}(.*)\]\]'
match = re.match(reg, text)
# You grab items by index. Starting from 1, 0 is the entire match
item1 = match.group(1)
item2 = match.group(2)

You will probably have to play with it a bit to get it to match what you want.

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

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.