简体   繁体   中英

Regular expressions to select text from string in Python

I have formatted lines of the text, ie

[[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. ???

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM