RegEx parsing?

Hi guys,

I’m quite new to regEx parsing and wonder what the best way to get each “parameter” block out of this file, with the equivalent data:

parameter = Tilt
default = 128
highlight = 128
crossfade = 0
type = ltp8bit
range = 0, 255, %

parameter = Colour
default = 0
highlight = 0
crossfade = 1
type = ltp8bit
range = 242, 254, spin >>
range = 0, white
range = 16, red
range = 32, blue
range = 48, green
range = 64, yellow
range = 80, cyan
range = 96, orange
range = 112, magenta
range = 128, neon grn
range = 144, pink
range = 160, UV blue
range = 176, aqua[/code]

Any ideas? Thanks!

If the structure of the data is consistent (parameter name, space, equals, space, data), this pattern will do it:

]+ = .+$"


An app like RegExRX can help you craft and test the right pattern for cases like this.


Seconded. Get RegExRX - one of the best purchases I ever made!

I already purchased RegExRx, but I seem to be kinda to dumb to find my way through regEx at the moment… haha… :wink: Is there any way to get data sorted in groups like with {parameter} or something?

Oh, sorry, I should have written it this way:

]+) = (.+)$"

SubexpressionString( 1 ) will have the parameter, SubexpressionString( 2 ) will have the data.

Do you need the code in its entirety?

Hi Kem,

thanks a lot! I guess I’ll leave the code part to be figured out myself :wink:
You’ve helped me so much already I dont want to waste your time :wink:

No biggie, I can do most of it right from RegExRX:

dim params() as string
dim values() as string

dim rx as new RegEx
rx.SearchPattern = "(?mi-Us)^([^=\\r\
]+) = (.+)$"

dim match as RegExMatch = rx.Search( sourceText )

while match <> nil
  params.Append rx.SubexpressionString( 1 )
  values.Append rx.SubexpressionString( 2 )

  match = rx.Search()

This will give you arrays with the parameters and matching values.

ummm … you really don’t need a regex for that
that’s using a sledgehammer to crack a peanut :stuck_out_tongue:

split the left hand side from the right using split on =
split the right hand side on , (unless of course your data includes , then you have a real problem in either case)

If the data contains an “=”, Split, NthField, etc., will require more steps. RegEx is “safer” and easier if that case might exist.

instr use the first one to get the “key portion”
mid past instr+1 to get the rest
heck you can get away with ^([^=])\=(.)$ for a regex IF the data is always as presented