What is the purpose of an empty parenthesis "()" in a regular expression?

7

Searching in Stackoverlow English on regular expressions, I came across a certain expression where it presents an empty group in it (an empty parenthesized expression).

So:

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

What is the purpose of this "empty group" ( () ) in a regular expression?

    
asked by anonymous 02.02.2016 / 15:32

3 answers

12

Depending on the case just confuse who is reading the regex. Parentheses indicate group capture, so in the expression (DE)()([0-9]{1,12}) DE will be captured in the first group, in the second we will capture nothing and in the third [0-9]{1,12} , where each group can be referenced by $numerodogrupo (in fact it depends on the regex engine, some do not use dollar sign), then we have three groups: $ 1, $ 2 and $ 3. Practical example, invert the text DE25324534 using the regex you passed:

var str = 'DE25324534';
var inverted = str.replace(/(DE)()([0-9]{1,12})/, '$3$1$2');
document.write(inverted);
What happens there is that the original string is replaced by $ 3 (the digits) followed by the $ 1 (DE) group, followed by the $ 2 group (there is nothing inside), so you can see that () loose on the regex serves for absolutely nothing in this case , however it may make sense in some situations as shown in the Guilherme Lautert .

    
02.02.2016 / 15:48
6

A priori, none.

Parentheses are usually used to identify groups. Groups, in turn, are used for operations such as extraction of specific information or location of areas for substitution of the considered string.

In this case, the regular expression engine will only create an empty group, which can not even be found.

    
02.02.2016 / 15:46
4

Complementing the Gypsy Response .

Group are used for information extraction or reuse. A% void group refers to nothing which in compiler would be the same as a direct transition to the next stage.

Example (bad, but demonstrative)

var replace = '$1$3$5';

'2016/02/02'.replace(/(\d{4})(\/)(\d{2})(\/)(\d{2})/, replace); // 20160202
'2016-02-02'.replace(/(\d{4})(-)(\d{2})(-)(\d{2})/, replace);   // 20160202
'20160202'.replace(/(\d{4})()(\d{2})()(\d{2})/, replace);       // 20160202
    
02.02.2016 / 16:37