regular expression replacement - reference followed by number

Asked

Viewed 986 times

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?

  • @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?

  • $12\3, group 12 followed by number 3 would be that?

  • @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.

  • 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 I am using the Sublime for the same, but if it is PHP would be the same thing, I believe Python also, but I’m not sure.

  • @Marcosregis I did the PHP text with preg_replace('~(\|C405\|\d+\|)\d~', '$13', $string) and he cut me all up pattern as well as did not insert the 3. sublime. is C++

  • @Guilhermelautert I have removed my comment pq really in some Engineering it generates an empty group when trying to use a non-existent grouping.

Show 3 more comments

2 answers

2


Short answer: depends on implementation.

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

In the Netbeans (which is done in Java) the replacement was done correctly with $1\2, escaping the next number.

In the editor sublime and in the geany the replacement was made as expected with \13 because the references go from 0 to 9.

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

In Javascript the replacement with $13 works as long as there is no group of number 13.

  • 1

    Your reference to ${{digito}} worked, both in PHP and in sublime.

1

For the question of entering a NOTHING can do like this

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

Note that I used 1 for replace instead of $1. The Empty Grouping worked on all REGEX Engins I tested, however the use of $ or may vary from engine to engine.

In Javascript no longer works above item but you can put a null character (ex.: x01) that is not added 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");
  • I hadn’t really thought of this logic of calling a group without catching. In JS it could be like this var a = "123456789"; a.replace(/()(\d+)/g,"$2$13");, taking advantage of the context @sanction commented on not existing group 13.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.