substitution with regular expression - reference followed by number

4

Situation

I was performing a replace of some data when I came across a peculiarity.

|C405|01102015|7|1058|174544|19836903,14|18657,06|
|C405|02102015|2|1059|174792|19859872,19|22441,55|
|C405|03102015|3|1060|174953|19872892,09|12993,90|
|C405|05102015|5|1061|175186|19893103,21|20126,12|
|C405|06102015|3|1062|175409|19914579,78|20500,57|
|C405|07102015|7|1063|175616|19937968,35|23388,57|
|C405|08102015|2|1064|175800|19954350,96|16382,61|
|C405|09102015|1|1065|176034|19975441,21|20483,75|
|C405|10102015|7|1066|176189|19987132,54|11570,33|
|C405|13102015|2|1067|176422|20010561,52|23052,98|
|C405|14102015|3|1068|176629|20033609,96|23020,44|
|C405|15102015|5|1069|176809|20054577,77|20885,81|
|C405|16102015|1|1070|177020|20077339,30|22456,53|

When mounting this regex :

pattern : (\|C405\|\d+\|)\d
replace : $13

Here I have an error, because my intention is to do the replace of group 1 and after inserting the literal 3, but the same is interpreting as group 13, which does not exist. And even though I change the replace to $1 , the editor tries to capture both group 1 and group 3, as it interprets both ${digito} and \{digito} as mirrors.

The solution to this specific problem is quite simple:

pattern : (\|C405\|\d+)\|\d
replace : $1\|3

Question

Let's say that I do not have this solition, that I should capture a group and then insert a digit , such as inserting NOTHING in the middle of the terms? $1{NADA}3

Obs

Without performing two REGEX operations:

pattern : (\|C405\|\d+\|)\d
replace : $1\x023

pattern : \x02
replace : 
    
asked by anonymous 06.11.2015 / 14:15

2 answers

2

Short answer: depends on implementation.

In the gedit editor ( Python syntax ) was accepted as a replacement: \g<1>3 .

In Netbeans (which is done in Java) the substitution was made correct with $1 , escaping the next number.

In the sublime editor and in the geany the replacement was made as expected with since references range from 0 to 9.

In PHP the documentation points to the use of keys: ${1}3 . sublime also accepts this syntax (as pointed out by @GuilhermeLautert).

In Javascript, the substitution with $13 works, as long as there is no number 13 group.

    
06.11.2015 / 17:11
1

For the question of inserting a NOTHING can do so

pattern : ((?:))(\|C405\|\d+)\|\d
replace : $2

Notice that I used \ 1 for replace instead of $ 1. The empty grouping worked on all the REGEX engines I tested, however the use of $ or \ may vary from engine to engine.

In Javascript it does not work above item but you can put a null character (eg: \ x01) that is not added to the final string.

var a = "0000111222333444555666777888999";
a.replace(/(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d+)/g,"$1\x013");
    
06.11.2015 / 17:44