Page 1 of 1

help #2 (thnx!)

Posted: Fri Dec 12, 2014 11:21 am
by woodpecker
Hi all,
forgive to me but I need a help with Reg Ex.
in the following matrix of data (space delimited), I need:
1) to transform any numeric value ".xyz" in "0.xyz" (preserving the 1.000)

than, about column headers (usually the first lane of the matrix) and the same in the first column of the matrix I need:
2) to transform any "1:abcdefghijklm" in "0001:abcdefghijklm"; "2:abcdefghijklm" in "0002:abcdefghijklm"; . . . "9:abcdefghijklm" in "0009:abcdefghijklm"
and so on for all the "one digit numbers"
3) to transform any "10:abcdefghijklm" in "0010:abcdefghijklm"; "11:abcdefghijklm" in "0011:abcdefghijklm"; . . . "99:abcdefghijklm" in "0099:abcdefghijklm"
and so on for all the "two digit numbers"
4) to transform any "100:abcdefghijklm" in "0100:abcdefghijklm"; "101:abcdefghijklm" in "0101:abcdefghijklm"; . . . "999:abcdefghijklm" in "0999:abcdefghijklm"
and so on for all the "three digit numbers"

many thanks in advance
all the best
a.b.

matrix:

1:ADI0001aMA2d2 2:ADI0001aMA2n1 3:ADI0004aMA2n1 <snip>10:ADI0023cMA2n2 11:ADI0023cMA2p3 12:ADI0026aMA2n3 <snip>100:BRE0724aMA2h2:
1:ADI0001aMA2d2 1.000 .909 .750 .824 .844 .658 .730 .730 .703 .352 .438 .824 .829 .771 .794 .743 .848
2:ADI0001aMA2n1 .909 1.000 .778 .800 .818 .684 .711 .711 .684 .370 .429 .800 .806 .750 .824 .722 .824
<snip>
10:ADI0023cMA2n2 .352 .370 .345 .304 .321 .370 .339 .364 .370 1.000 .633 .352 .364 .352 .358 .385
11:ADI0023cMA2p3 .438 .429 .429 .408 .467 .458 .449 .511 .522 .633 1.000 .500 .479 .468 .478 .478
12:ADI0026aMA2n3 .824 .800 .750 .824 .844 .703 .730 .829 .800 .352 .500 1.000 .829 .824 .848 .848
<snip>
100:BRE0724aMA2h2 .540 .529 .560 .510 .510 .560 .519 .519 .500 .492 .474 .510 .549 .540 .551 .551
101:BRE0726bMA2n3 .667 .690 .690 .667 .675 .732 .600 .636 .614 .473 .540 .750 .756 .667 .769 .725
<snip>
1001:VMA0011dMA2s3 .583 .604 .604 .583 .553 .604 .592 .625 .604 .526 .566 .652 .660 .583 .596 .596
1002:VMA0013dMA2s3 .610 .595 .595 .692 .615 .675 .700 .700 .675 .453 .521 .650 .700 .610 .625 .585

Posted: Fri Dec 12, 2014 9:55 pm
by ben_josephs
1.
Find what: _\. [Replace the underscore with a space]
Replace with: _0. [Replace the underscore with a space]
2.
Find what: \b\d:
Replace with: 000$0
3.
Find what: \b\d\d:
Replace with: 00$0
4.
Find what: \b\d{3}:
Replace with: 0$0

Posted: Sun Dec 14, 2014 8:46 pm
by woodpecker
Magic! thanks a lot!
the solution 1 works with a small adaptation as there are no 'space' ('_') before the dots ('.xyz' instead of '_.xyz'). the expression FIND '\.' and REPLACE WITH '0\.' do not preserve the '1.000', resulted modified into '10.000', but the last can be easily undo by means of the following:
FIND '10\.000' and REPLACE WITH '1\.000'.

any improvement of the RegEx solution 1, to avoid the "undo" passage is welcome.

solution 2, 3 and 4 works great!
On the other hand, it is never enough... ;) and this is for to ask if it is possible to combine steps 1+2+3+4 in a single RegEx to reach the same goal. I have tried with the '|' (the OR metacharacter) without success.
Hoping it can be useful for most of the community
many thanks in advance
w.p.

Posted: Sun Dec 14, 2014 9:27 pm
by ben_josephs
1.
Find what: (\D)\.
Replace with: ${1}0.
2, 3, 4.

Yes, it can be done, but you have to use conditional replacement expressions, which are somewhat cumbersome. Try
Find what: (\b\d:)|(\b\d\d:)|(\b\d{3}:)
Replace with: (?1(000$0):?2(00$0):?3(0$0))
or
Find what: \b(?:(\d)|(\d\d)|(\d{3})):
Replace with: (?1(000$1):?2(00$2):?3(0$3)):
or something similar.