import re s = r'''\newcommand{\commandName1}{This is first command} \newcommand{\commandName2}{This is second command with {} brackets insidein multiple lines {} {}} \newcommand{\commandName3}{This is third, last command}''' print (re.findall(r'(?s)\\newcommand{([^}]+)}{(.+?)}(?=\s*(?:\\newcommand|$))', s))
Standard input is empty
[('\\commandName1', 'This is first command'), ('\\commandName2', 'This is second command with {} brackets inside\nin multiple lines {} {}\n'), ('\\commandName3', 'This is third, last command')]
The brand new service which powers Ideone!
Widget for compiling and running the source code in a web browser!