Demi Demi - 1 year ago 76
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?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download