Skip to content

Commit 735dc52

Browse files
authored
use multiline regex (#402)
* use multiline regex' * re.escape * doctest pattern too
1 parent 2782a28 commit 735dc52

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

nbqa/__main__.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -141,18 +141,18 @@ def _map_python_line_to_nb_lines(
141141
Stdout with references to temporary Python file's lines replaced with references
142142
to notebook's cells and lines.
143143
"""
144-
pattern = rf"(?<={notebook.name}:)\d+"
144+
pattern = rf"(?<=^{re.escape(str(notebook))}:)\d+"
145145

146146
def substitution(match: Match[str]) -> str:
147147
"""Replace Python line with corresponding Jupyter notebook cell."""
148148
return str(cell_mapping[int(match.group())])
149149

150-
out = re.sub(pattern, substitution, out)
150+
out = re.sub(pattern, substitution, out, flags=re.MULTILINE)
151151

152152
# doctest pattern
153-
pattern = rf'(?<={notebook.name}", line )\d+'
154-
if re.search(pattern, out) is not None:
155-
out = re.sub(pattern, substitution, out)
153+
pattern = rf'(?<=^File "{re.escape(str(notebook))}", line )\d+'
154+
if re.search(pattern, out, flags=re.MULTILINE) is not None:
155+
out = re.sub(pattern, substitution, out, flags=re.MULTILINE)
156156
out = out.replace(f'{notebook.name}", line ', f'{notebook.name}", ')
157157

158158
return out

0 commit comments

Comments
 (0)