Page 1 of 1
Splitting lines
Posted: Mon Jan 12, 2009 4:24 pm
by corny
Hi,
I want to split textlines into multiple lines:
Code: Select all
SetPlanTijden(fc98, PL1, 80, 80, 3, 3, 0);
The result should be:
Code: Select all
TXA PL1 98 = 80
TXB PL1 98 = 80
TXC PL1 98 = 3
TXD PL1 98 = 3
TXE PL1 98 = 0
However, my textfile also contains lines like these:
Code: Select all
SetPlanTijden2R(fc45, PL1, 33, 33, 0, 41, 41, fc45_2, 6, 6, 0, 14, 14);
This line should be splitted as follows:
Code: Select all
TXA PL1 45 = 33
TXB PL1 45 = 33
TXC PL1 45 = 0
TXD PL1 45 = 41
TXE PL1 45 = 41
TXA PL1 45_2 = 6
TXB PL1 45_2 = 6
TXC PL1 45_2 = 0
TXD PL1 45_2 = 14
TXE PL1 45_2 = 14
Is there a way to do this in one regex find&replace?
Any help is appreciated!
Posted: Mon Jan 12, 2009 4:37 pm
by talleyrand
No, I don't believe regex would be able to handle a variety of things you are doing there. The first one that jumps out is incrementing by characters (A-B-C...) The the logic of when to split (45 vs 45_2) would be troublesome. A scripting language would probably be more suitable to your needs.
Posted: Tue Jan 13, 2009 12:35 am
by Bob Hansen
I think that TextPad RegEx can do this, but don't have time to try it now. My only concern is if we are limited to \1-\9 and it looks like we need 10 variables vs. 9. This means it will need to take two passes.
The key is to put the values in () for \1-\9 and then hard code the 10 lines vs incrementing the letters
Something like this with POSIX:
Search for: SetPlanTijden2R(fc45, PL1, ([0-9][0-9]), ([0-9][0-9] etc.
Replace with
TXA PL1 45 = \1\nTXB PL1 45 = \2\nTXC PL1 45 = \3\nTXD PL1 45 = \4\nTXE PL1 45 = \5\n\nTXA PL1 45_2 = \6\nTXB PL1 45_2 = \7\n etc.
ben_josephs will probably set this up in just a few minutes. Will take me much longer, no time to do it now.
Other questions:
Will the value always be 45 or is that also a variable?
Will the PL be a constant or is that also a variable?
Will the number digits always be 1 or 2 in the positions shown, or what range could they be?
Posted: Tue Jan 13, 2009 7:19 am
by corny
Thanks for your answers. If I have some spare time I will put this into a small C program, I think that's easier... Also because all other lines in the file which don't contain these texts should be deleted, so multiple parsing will be necessary. Or not??
Regarding Bob's questions:
Each item between the parentheses is a variable argument for the functions SetPlanTijden and SetPlanTijden2R.
The number digits could vary from 0 to 999.
Furthermore, there is no convention for using spaces in front of the function or in the arguments, so each argument could be proceeded by spaces. Or not...
Posted: Tue Jan 13, 2009 9:39 am
by ben_josephs
I see you're now looking for a different solution. But here is what I was going to write.
You can do it in two steps:
1. Convert the longer lines into pairs of shorter lines:
Find what: ^([^(]+)\( *([^,]+), *([^,]+)(.*).*, *fc([^,]+)
Replace with: \1(\2, \3\4);\n\1(fc\5, \3
[X] Regular expression
Replace All
2. Split the shorter lines:
Find what: ^[^(]+\( *fc([^,]+), *([^,]+), *([^,]+), *([^,]+), *([^,]+), *([^,]+), *([^,]+)\);
Replace with: TXA \2 \1 = \3\nTXB \2 \1 = \4\nTXC \2 \1 = \5\nTXD \2 \1 = \6\nTXE \2 \1 = \7\n
[X] Regular expression
Replace All
These assume you are using Posix regular expression syntax:
Configure | Preferences | Editor
[X] Use POSIX regular expression syntax
But I would
not do it this way. I would use a script.
Posted: Tue Jan 13, 2009 3:12 pm
by Bob Hansen
Thanks Ben for confirming my thought that it was doable.
But looking at your solution, I think it would have taken me three times longer than my original estimate to come up with this.
Thanks for the great example and the tutorial. I like how you used anything not a comma vs. [0-9] blocks, much better approach.
Posted: Tue Jan 13, 2009 3:22 pm
by corny
The spare time was found today and I started my first C# project
The program works great, but probably use of regex could make my code more efficient than now.
However, thanks for your efforts in helping me.