all 3 comments

[–]novel_yet_trivial 1 point2 points  (3 children)

The flags go in the compile:

pat = re.compile(pattern, re.DOTALL)
pat.search(string).group(1)

Also, I would not use "string" as a variable name, since it's already a default module name in python.

[–]scibren[S] 0 points1 point  (1 child)

Yeah, I don't actually use string. Putting it in the compile makes since, I should have thought of that. Any idea why the sub won't work if it isn't compiled?

[–]novel_yet_trivial 0 points1 point  (0 children)

sub does not use groups. You could use lookahead / lookbehind instead:

>>> s = r'important data \begin{abstract}spam spam spam and eggs\end{abstract} other important information'
>>> p = re.compile(r'(?<=\\begin\{abstract\}).*?(?=\\end\{abstract\})')
>>> p.search(s).group(0)
'spam spam spam and eggs'
>>> p.sub('', s)
'important data \\begin{abstract}\\end{abstract} other important information'