I'm working with some marked text and I need to extract information in order to use later. I want to use regular expressions from Python using the module re, but I can't construct the right expression. I have two situations:
Text in format
string="{some text}{other text 1}{other text 2}". Here I use the regexp"\\{(.*?)\\}"but I obtain>> string="{some text}{other text 1}{other text 2}" >> elements = re.split("\\{(.*?)\\}",string) >> print(elements) >> ['', 'some text', '', 'other text 1', '', 'other text 2', '']I can't understand why the empty strings appear in positions 0, 2, 4 and 6. If I edit my original string to
string="}{some text}{other text 1}{other text 2}{"and use the regexp"\\}\\{(.*?)\\}\\{"I obtain>> string="}{some text}{other text 1}{other text 2}{" >> elements = re.split("\\}\\{(.*?)\\}\\{",string) >> print(elements) >> ['', 'some text', 'other text 1', 'other text 2', '']the internal empty strings in the output dissapear, but not the first and last. How should I construct the regular expression in order to obtain only the elements inside brackets?
Text in format
string="some text {other text}". In this case I need to extract"some text"and also"other text". Here I don't know how to proceed.
Can someone help me, please?
"1,2,3".split(",")and asking why there are numbers in the result.re.findall(r"\{([^}]+)\}",string). Notice ther""for a raw string, so you do not have to escape\. Also[^}]+is cleaner than.*?since it can never overstep a closing curly bracket.