PDA

View Full Version : Help in Search and Replace using Wildcards



swaggerbox
02-16-2013, 03:54 AM
Paul did an excellent in getting the regex pattern in my previous post (http://www.vbaexpress.com/forum/showthread.php?t=45197).

Find = (<[0-9.]{1,}>) part[s ]{1,2}of (<*> ink)
Replace = \2 (\1)

I want something similar but this time, the pattern is a bit variable. There is a mixture of whole numbers and decimals, and numbers could go up the thousands level. Could anyone help me with this?

Original: (input)
40-120 parts of isoleucine, 45-135 parts of leucine, 76.5-229.5 parts of lysine hydrochloride, 21.5-64.5 parts of methionine, 35-105 parts of phenylalanine, 40-120 parts of valine, 30-90 parts of threonine, 39-117 parts of arginine, 23-69 parts of histidine, 37.5-112.5 parts of glycine, 50-150 parts of aspartate, 900-2700 parts of dried mushroom, 750-2250 parts of medlar and 250-750 parts of liquorice.

Modified: (Desired output)
isoleucine (40-120), leucine (45-135), lysine hydrochloride (76.5-229.5), methionine (21.5-64.5), phenylalanine (35-105), valine (40-120), threonine (30-90), arginine (39-117), histidine (23-69), glycine (37.5-112.5), aspartate (50-150), dried mushroom (900-2700), medlar (750-2250) and liquorice (250-750).

EricFletcher
02-16-2013, 08:35 AM
You'll need to do this in 3 parts to manage the "and" part separating the penultimate and final terms.

First, flag instances of the penultimate “and” to make them consistent with the others, but also so they can later be restored:
Fw: ( and )(<[0-9.]{1,}>)(-)(<[0-9.]{1,}>)( part[s ]{1,2}of )
Rw: ώ, \2\3\4\5and
(be sure to include the space after the "and" above in the replace with exression)

Now do the replace to get all the value ranges within parentheses following the terms:
Fw: (<[0-9.]{1,}>)(-)(<[0-9.]{1,}>)( part[s ]{1,2}of )(*)([,.])
Rw: \5 (\1\2\3)\6

Finally, restore the penultimate “and” by removing the flag and comma:
Fw: (ώ )(\(<[0-9.]{1,}>)(-)(<[0-9.]{1,}>)(\),)
Rw: \2 \3\4)
(edit: be sure to include a space before the "\2" above)

This is certainly an interesting and challenging use of wildcards and shows just how much can be done to parse out a pattern!

swaggerbox
02-16-2013, 04:21 PM
thanks a lot eric! this is great! this is certainly very creative

macropod
02-17-2013, 04:24 AM
Actually, you can do it all with just two wildcard Find/Replace expressions:
Find = (<[0-9\-.]{1,}>) part[s ]{1,2}of (<*>)
Replace = \2 (\1)
Find = ( \([! ]@[\)])([!,.^13\)]@)([,.^13])
Replace = \2\1\3

swaggerbox
02-17-2013, 04:17 PM
thanks paul. great to have you again!