Skip to content

Instantly share code, notes, and snippets.

@malfet
Last active June 29, 2023 15:29
Show Gist options
  • Save malfet/5cec3910be0a3ee0bf3a496ebd687b99 to your computer and use it in GitHub Desktop.
Save malfet/5cec3910be0a3ee0bf3a496ebd687b99 to your computer and use it in GitHub Desktop.
Catastrophic backtracking in regexp
# For some reason does not work when copied-an-pasted not as raw file, but otherwise shoudl hang
import re
pat=re.compile('\\.\\. (code-block|math)::.*$\\n*(?P<S2VCUH>(?P<first>(^(?P<indent>[ ]+).*$\\n))(?P<other>(^([ \\t]+.*|[ \\t]*)$\\n)*))(?:(^(?![ \\t]+.*$))|\\Z)', re.MULTILINE)
text="""#####################################################################
We get the following performance profiling table for the eager-mode model (omitting some columns):
.. code-block:: shell
------------------------- ------------ ------------ ------------ ------------
Name CPU total % CPU total CPU time avg # of Calls
------------------------- ------------ ------------ ------------ ------------
aten::addmm 45.73% 370.814ms 1.024ms 362
aten::add 19.89% 161.276ms 444.287us 363
aten::copy_ 14.97% 121.416ms 248.803us 488
aten::mul 9.02% 73.154ms 377.082us 194
aten::clamp_min 8.81% 71.444ms 744.208us 96
aten::bmm 5.46% 44.258ms 922.042us 48
ProfilerStep* 100.00% 810.920ms 810.920ms 1
aten::div 2.89% 23.447ms 976.958us 24
aten::_softmax 1.00% 8.087ms 336.958us 24
aten::linear 46.48% 376.888ms 1.041ms 362
aten::clone 2.77% 22.430ms 228.878us 98
aten::t 0.31% 2.502ms 6.912us 362
aten::view 0.14% 1.161ms 1.366us 850
aten::transpose 0.17% 1.377ms 3.567us 386
aten::index_select 0.12% 952.000us 317.333us 3
aten::expand 0.12% 986.000us 2.153us 458
aten::matmul 8.31% 67.420ms 1.405ms 48
aten::cat 0.09% 703.000us 703.000us 1
aten::as_strided 0.08% 656.000us 0.681us 963
aten::relu 8.86% 71.864ms 748.583us 96
------------------------- ------------ ------------ ------------ ------------
Self CPU time total: 810.920ms """
print(pat.match(text, pos=172))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment