http://www.perlmonks.org?node_id=394886


in reply to Stuck with RegEx (Parsing whois output)

SOLVED

Hello folks,

I finally made it, not too good, but proper. I think there ain't no solution for that problem with one single match as I tried, so I'll use this workaround as I may assume there is a finite number of equal key words:

@address = $whois =~ m/(?:\{section1\}(?:\{section[\d]+\})*)\n(?:\S*(? +<!key9):\s+[\S]+[^\n]+\n)+(?:key9:\s+([\S]+[^\n]+)\n)(?:key9:\s+([\S] ++[^\n]+)\n)?(?:key9:\s+([\S]+[^\n]+)\n)?(?:key9:\s+([\S]+[^\n]+)\n)?( +?:(?<!key9):[^\n]+\n)*?/gs; for (my $c = scalar @address; $c > 0; --$c) { pop @address unless defined $address[$c - 1]; }

This is the matched file format:

...
key6: key6_1

{section1}
key7: key7_1_section1
key8: key8_1_section1
key9: key9_1_section1
key9: key9_2_section1
key9: key9_3_section1
key10: key10_1_section1
key11: key11_1_section1
key12: key12_1_section1
key16: key16_1_section1
key17: key17_1_section1

{section2}{section3}
key7: key7_1_section2_section3
...