Slightly complicated search and replace?

Soldato
Joined
18 Jan 2006
Posts
3,179
Location
Norwich
Apologies if this sounds a little complicated :)
I've got halfway towards where I'm trying to go, but have got a little stuck.
In essence, I have several plaintext files. I want to find certain strings in these, and then perform a replace operation on a subset of the string- This is where I fall down.

An small section of the file I'm working on looks like this:
Code:
2000, .railstart 2;26;0;0, .railstart 4;30;0;0, .dike 1;1;4, .wall 0;-1;11
2025, .rail 2;22, .rail 4;26, .freeobj 0;179;1, .freeobj 0;1002;0.5;6.3, .freeobj 0;52;8;-0.8;-90, .SigF 3;1;-2;6.3, .Section 0; 2; 4,
2050, .rail 2;18, .rail 4;22, .railtype 1;18
2060, .freeobj 0;366;13;-0.8;-100
2075, .rail 1;4.7;0;0, .rail 2;14, .rail 4;18
2091, .freeobj 2;3;0;-0.01
2100, .rail 1;5.33;0;5, .rail 2;10;0;1, .rail 4;14, .railstart 5;0;-0.1;1, .wallend 0, .wall 5;-1;103, .dike 5;1;4
2115, .freeobj 5;13;-6;-1;95
2125, .railtype 1;1, .rail 2;6, .rail 4;10, .rail 5;-3, .freeobj 5;13;-4;-0.3;95
2126, .freeobj 2;3;0;-0.01
2135, .freeobj 0;368;-3;-0.8;90
I've created a regexp which matches the to the desired string. This looks like this:
Code:
\.freeobj \d{1,3};99[, ;]
The part I'm interested in replacing is the second number in the .freeobj strings (This varies as does the total length of the .freeobj strings, but I'm planning on running whatever script comes out of this multiple times to handle each individual number).
The issue I keep running into is basically that I can't find a decent way to handle the varying initial number. The regexp matches to it nicely, but I can only run a static replacement.

Cheers :)

-Leezer-
 
Sorted myself with a little further reading :)
Capturing groups are useful little devils, so applied to my problem:

Revise the regexp so that it's split into three groups:
Code:
(\.freeobj \d{1,3})(;4)([, ;])

Then setup a replacement like this:
Code:
\1;1020\3

Very neat :)

-Leezer-
 
Back
Top Bottom