Why not create a backreference?

大城市里の小女人 提交于 2019-12-23 13:39:05

问题


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? Under 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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/5293986/why-not-create-a-backreference

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!