Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks,

I'm trying to parse a string and capture chunks of data for every 4 dots like so:

data = 12d.9j7h.7tttt.125.ty.2.4.8.9f.y6.34gk.ll after parse = 12d.9j7h.7tttt.125 ty.2.4.8 9f.y6.34gk.ll
I've tried splitting the string on the dot, pushing it onto an array, counting the dots until I reach 4, reassembling the chunk and then pushing it onto another array. This seems like it should be much easier, any suggestions?

Thanks

Replies are listed 'Best First'.
Re: regex needed for dot delimited string
by Abigail-II (Bishop) on Sep 10, 2003 at 10:58 UTC
    Assuming there are no empty strings between the dots:
    @chunks = $str =~ /[^.]+(?:[.][^.]+){0,3}/g;

    Abigail

Re: regex needed for dot delimited string
by broquaint (Abbot) on Sep 10, 2003 at 11:04 UTC
    $_ = '12d.9j7h.7tttt.125.ty.2.4.8.9f.y6.34gk.ll'; print "split: ", map "[$_]", grep $_, split /((?:\w+.){3}\w+)\.?/; print "regex: ", map "[$_]", m< ( (?: \w+. ){3} \w+ ) \.? >xg; __output___ split: [12d.9j7h.7tttt.125][ty.2.4.8][9f.y6.34gk.ll] regex: [12d.9j7h.7tttt.125][ty.2.4.8][9f.y6.34gk.ll]
    Using split we use the capturing feature and grep out the extraneous data to get the desired output. With the regex we do something similar by capturing all the groups in the string. So it would seem, in this case the regex approach would be better.

    See. perlre and split for more info.
    HTH

    _________
    broquaint

    update: fixed code (wasn't dealing with the last group correctly)

      The last character (an "l") in last group in both output strings is missing. (Are you sure you can use the dot in the regexp without escaping it?)
Re: regex needed for dot delimited string
by Anonymous Monk on Sep 10, 2003 at 11:35 UTC
    Thanks,
    I always learn something new on this site...Thanks again