Hi All,
I'm trying to make modifications to industrial control ladder logic and have been quite successful so far. I'm now stumped at trying to create a regex to remove some code.
A bit of background so you understand what I'm trying to do. The code is essentially a big if-then statement.
For example,
N: [XIC(bit_a), XIC(bit_b) XIC (bit_c)] OTE (bit_e);
Means
new rung, IF (bit_a == 1 OR (bit_b == 1 AND Bit_c == 1)) then bit_e = 1 end of rung
What I'm trying to do is take out the second OR condition. The pseudo-regex is
"Opening [" (intervening code into capture group 1) ", OR condition closing ]" (remaining code ; into capture group 2)
and replace with
Capturegroup1 Capturegroup2
This works fine as long as (intervening code into capture group 1) doesn't contain any parallel branches. If it does, I'm capturing the entire code from the (incorrect)opening bracket to the correct closing one. What I need to do is find my OR condition and attached closing bracket and capture up to the matching opening bracket.
Example Working Test Data
Original data:
N: [XIO(F7_72_0752.HMI.Status.Auto) ,XIC(AlarmSeverity4.Active[6].18) XIO(AlarmSeverity4.Ack[6].18) ]OTE(AlarmSeverity4.Active[6].18);
Using regex replace
\[(.*?),XIC\(AlarmSeverity[\d]\.Active\[[\d]{1,2}\]\.[\d]{1,2}\) XIO\(AlarmSeverity[\d]\.Ack\[[\d]{1,2}\]\.[\d]{1,2}\) ]
With
\01
Results in
N: XIO(F7_72_0752.HMI.Status.Auto) OTE(AlarmSeverity4.Active[6].18);
which is correct.
Using orignial the original data With an extra OR condition (and so extra opening square bracket)
N: [XIC(F7_72_0714.GY.A) XIO(F7_72_0714.GS.H) TON(F7_72_0714.HMITimers[0].Timer,?,?) ,[XIC(F7_72_0714.HMITimers[0].Timer.DN) ,XIC(AlarmSeverity4.Active[8].21) XIO(AlarmSeverity4.Ack[8].21) ] OTE(AlarmSeverity4.Active[8].21) ];
With the same regex search and replace results in
N: XIC(F7_72_0714.GY.A) XIO(F7_72_0714.GS.H) TON(F7_72_0714.HMITimers[0].Timer,?,?) ,[XIC(F7_72_0714.HMITimers[0].Timer.DN) OTE(AlarmSeverity4.Active[8].21) ];
Whereas it should be
N: [XIC(F7_72_0714.GY.A) XIO(F7_72_0714.GS.H) TON(F7_72_0714.HMITimers[0].Timer,?,?) ,XIC(F7_72_0714.HMITimers[0].Timer.DN) OTE(AlarmSeverity4.Active[8].21) ];
See how it's taken the opening square bracket at the beginning of the line, instead of the balancing one before ",[XIC(F7_72_0714"?
the Regex
,XIC\(AlarmSeverity[\d]\.Active\[[\d]{1,2}\]\.[\d]{1,2}\) XIO\(AlarmSeverity[\d]\.Ack\[[\d]{1,2}\]\.[\d]{1,2}\) ]
accurately selects the condition I'm after, but I need to capture the matching opening square bracket for the one I've just captured above.
I'm using powerGREP and RegexBuddy.
Many thanks,
Kevin Jones