regexp per se can not match recursive structures. for that you need a
context free grammar and an appropriate parser. i think there are parser
generators for ruby.
one thing you might consider as you seem to be using fixed width chunks
maiching with . is to use String#unpack to dismember the string and its
subcomponents. After that you could do some manyal packing.
If this happens more than once then you might consider using a more
sophisticated approach with some higher level tools as another poster has
suggested.
Unpack is usually easier to read (x means skip, a means non-null ASCII chars,
if you wantes the spaces trimmed from the NUT_SPRG-EXPN field then you
could use A rather than a.) E.g.
result = line.unpack(‘x4 a12 x17 a16 x13 a x33 a7 x17 a12’)
[0, -1].each { |i| result[i] = result[i].unpack(‘a3a6a3’) }
It isn’t hard to imagine writing a routine to let you use a modifed pack
specifier to show some structure e.g.
result = line.my_unpack(‘x4 [a3 a6 a3] x17 a16 x13 a x33 a7 x17 [a3 a6 a3]’)
Thanks a lot! That makes my program easier, because in the input file
specification, it is described as offset/length, so I used
unpack(“@4a3a6a3@17a16…”).
Shannon
···
On Thu, 12 Dec 2002 22:05:35 +0900 Mike Stok mike@stok.co.uk wrote:
It seems that result is flattened. I expect result like this:
[[a,b,c], d,e,f,[g,h,i]]
Could anyone tell me how can I achieve that? Thanks a lot!
one thing you might consider as you seem to be using fixed width chunks
maiching with . is to use String#unpack to dismember the string and its
subcomponents. After that you could do some manyal packing.
If this happens more than once then you might consider using a more
sophisticated approach with some higher level tools as another poster has
suggested.
Unpack is usually easier to read (x means skip, a means non-null ASCII chars,
if you wantes the spaces trimmed from the NUT_SPRG-EXPN field then you
could use A rather than a.) E.g.
result = line.unpack(‘x4 a12 x17 a16 x13 a x33 a7 x17 a12’)
[0, -1].each { |i| result[i] = result[i].unpack(‘a3a6a3’) }
It isn’t hard to imagine writing a routine to let you use a modifed pack
specifier to show some structure e.g.
result = line.my_unpack(‘x4 [a3 a6 a3] x17 a16 x13 a x33 a7 x17 [a3 a6 a3]’)