javascript - Regex - why empty parenthesis? - Stack Overflow

admin2025-04-09  2

I have a regex to update and there is an empty parenthesis in it. And i wondering : what is the purpose ? I don't find something about it.

The regex :

(DE)()([0-9]{1,12})

Because, if it is useless, i can remove it.

I have a regex to update and there is an empty parenthesis in it. And i wondering : what is the purpose ? I don't find something about it.

The regex :

(DE)()([0-9]{1,12})

Because, if it is useless, i can remove it.

Share Improve this question edited Sep 2, 2014 at 12:40 anubhava 787k67 gold badges596 silver badges664 bronze badges asked Sep 2, 2014 at 12:39 CharlieNoodlesCharlieNoodles 3562 silver badges9 bronze badges 5
  • 11 It doesn't match anything, but it will count as a capture group. – OrangeDog Commented Sep 2, 2014 at 12:41
  • 1 People make mistakes all the time. Where did you find this regex? Personally, I think it's useless. – Amal Commented Sep 2, 2014 at 12:45
  • 3 To expound a bit on @OrangeDog's ment, it's a rather hackish way to get other capture groups to line up with other regexes that may be used which actually capture information there. – RevanProdigalKnight Commented Sep 2, 2014 at 12:45
  • This regex is in my professional application source code, but i can't talk to the original writer of this :) – CharlieNoodles Commented Sep 2, 2014 at 12:46
  • Check your version control system for the history of the line. – raina77ow Commented Sep 2, 2014 at 12:49
Add a ment  | 

2 Answers 2

Reset to default 6

There is one possible application for empty parentheses that I'm aware of, and that is if you plan to use a regex to determine if a certain string matches a permutation of sub-regexes.

For example,

^(?:A()|B()|C()){3}\1\2\3$

will match ABC or CBA or BCA but not AAA or BCC etc.

But it doesn't look like that's what the author of your regex was going for.

Maybe (and only maybe) the other code uses the capturing groups by their numbers.

It happened to me that I changed one regex changing the parenthesis so the matching groups were changed as well and the rest of the code stopped working because depended on the number of the matching groups.

I remend you to verify if this is your case before removing the parenthesis.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1744203577a235914.html

最新回复(0)