views:

103

answers:

2
+1  Q: 

List of substrings

I have big string, it can have a few thousands lines. I would like to to get all sub-strings like: [tag] here can be everything [/tag] in a list.

How can I do this? My regex is not working (or I'm doing something wrong).

A: 

The function find_all_tags returns a list of all occurences of tag tag in text:

import re
def find_all_tags(text, tag):
    return re.findall(r"(?s)\[" + tag + r"\].*?\[/" + tag + r"\]", text)

>>> text="""this is [b]bold text[/b] and some[b]
that spans a line[/b] some [i]italics[/i] and some
[b][i]bold italics[/i][/b]"""
>>> find_all_tags(text, "b")
['[b]bold text[/b]', '[b]\nthat spans a line[/b]', '[b][i]bold italics[/i][/b]']

Tell me if you need something different (e.g a generator instead of a list of the substrings)

ΤΖΩΤΖΙΟΥ
I would like to know how can my answer be judged as “not useful” and be worthy of a downvote; AFAIU, it's exactly what the question requested.
ΤΖΩΤΖΙΟΥ
A: 

You can just use string splits

for item in my_big_string.split("]"):
    if "[" in item:
         print item.split("[")[-1]

eg

>>> text="""this is [b]bold text[/b] and some[b]
... that spans a line[/b] some [i]italics[/i] and some
... [b][i]bold italics[/i][/b]"""

>>> for item in text.split("]"):
...    if "[" in item:
...        print item.split("[")[-1],
...
b /b b /b i /i b i /i /b
>>>
ghostdog74