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|
By riding this regex
:
pattern : (\|C405\|\d+\|)\d
replace : $13
Here I have an error, because my intention is to replace the group 1 and after inserting the literal 3, but the same is interpreting as group 13, which does not exist. And even if I change the replace to $1\3
, the editor tries to capture group 1 and group 3, as it interprets both ${digito}
and \{digito}
as rearview mirrors.
The Solution to this specific problem is very simple :
pattern : (\|C405\|\d+)\|\d
replace : $1\|3
Question
Let’s say I don’t have this soling, that I should capture a group and then insert a digit, how to insert ANYTHING, 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 :
\13
solves your problem?– rray
@simple rray like this? yes solved, that is due to the fact of the
\{digito}
go from\1...\9
correct? then the second digit would be considered literal. But how would the solution be if the replace group is$12
for example?– Guilherme Lautert
$12\3
, group 12 followed by number 3 would be that?– rray
@rray there does not work, because
\3
is a rearview mirror, as I explained, so he tries to replace it with group 3. And keeping $123 doesn’t work either, as he interpreted as group 123.– Guilherme Lautert
I don’t know if you’re using any language or if it’s just a replace with some text editor, with the language, the first thing that comes to mind is to make an arbitrary replace only at the 12th occurrence. I’ll find something else.
– rray
@rray I am using the Sublime for the same, but if it is
PHP
would be the same thing, I believePython
also, but I’m not sure.– Guilherme Lautert
@Marcosregis I did the PHP text with
preg_replace('~(\|C405\|\d+\|)\d~', '$13', $string)
and he cut me all uppattern
as well as did not insert the 3. sublime. is C++– Guilherme Lautert
@Guilhermelautert I have removed my comment pq really in some Engineering it generates an empty group when trying to use a non-existent grouping.
– Marcos Regis