Demi Demi - 1 month ago 24
Perl Question

How can I match regexps like /\s*a\s*b/ without excessive backtracking?

I am working in Perl, which uses a backtracking regex engine.

I need to match strings of whitespace-delimited tokens (I am parsing assembler in case anyone is wondering). I am currently using regexps like

s/(\.text\n\s*\.align .(?:,0x90)?\n)\.globl\s+.*_?__stg_split_marker.*\n/$1/m

to do this, but am worried about excessive backtracking.

How can I prevent this?


The general approach is for any sub-expression that might do backtracking that you want to inhibit, surround the sub-expression with (?>...). So \s+ would be (?>\s+), for example.