开发者

Why not create a backreference?

I understand that putting ?: inside of the start of the parentheses of a regular expression will prevent it from creating a backreference, which is supposed to be faster. My question is, why do this? Is the speed increase noticeable enough to warrant this consideration? Un开发者_运维技巧der what circumstances is it going to matter so much that you need to carefully skip the backreference each time you are not going to use it. Another disadvantage is that it makes the regex harder to read, edit, and update (if you end up wanting to use a backreference later).

So in summary, why bother not creating a backreference?


I think you're confusing backreferences like \1 and capturing groups (...).

Backreferences prevent all kinds of optimizations by making the language non-regular.

Capturing groups make the regular expression engine do a little more work to remember where a group starts and ends, but are not as bad as backreferences.

http://www.regular-expressions.info/brackets.html explains capturing groups and back references to them in detail.

EDIT:

On backreferences making regular expressions non-regular, consider the following regular expression which matches lua comments:

/^--(?:\[(=*)\[[\s\S]*?(?:\]\1\]|$)|[^\r\n]*)/

So --[[...]] is a comment, --[=[...]=] is a comment, --[==[...]==] is a comment. You can nest comments by adding extra equals signs between the square brackets.

This cannot be matched by a strictly regular language, so a simple finite state machine cannot handle it in O(n) time -- you need a counter.

Perl 5 regular expressions can handle this using back-references. But as soon as you require non-regular pattern matching, your regular expression library has to give up the simple state-machine approach and use more complex, less-efficient code.


You're right, performance is not the only reason to avoid capturing groups--in fact, it's not even the most important reason.

Another disadvantage is that it makes the regex harder to read, edit, and update (if you end up wanting to use a backreference later).

I look at it the other way around: if you habitually use non-capturing groups, it's easier to keep track of the group numbers on those occasions when you do choose to capture something. In the same vein, if you're using named groups (assuming your regex flavor supports them), you should always use named groups, and always refer to them (in backreferences or replacement strings) by name, not by number. Following these rules consistently will at least partially offset the readability penalty of non-capturing groups.

Yes, it is a PITA having to clutter up your regexes that way, and the people who write/maintain the regex implementations know it. In .NET you can set the ExplicitCapture option whereby all "bare" parentheses are treated as non-capturing groups, and only named groups capture. In Perl 6, parentheses (with or without names) always capture, and square brackets are used for non-capturing groups. The other flavors will probably follow suit eventually, but in the meantime we just have to rely on good habits.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜