in reply to More Regular Expressions (text data handling)

It's hard to know how to parse this in a general sense if you won't tell us the "rules" that are used to produce the data. As it is, your dataset is very inconsistent.

Some lines have a field name and a value:

Number: 634321
Some lines have two field/value pairs, with no separator!
age: 27 hair colour: black
Some lines have a value, with no field name:
Graq Agnostic
And toward the end you have a series of values, followed by a series of field names:
jameson bells guinness favourite detests likes

If there are in fact any real rules governing the data, you should tell us. Better yet, you should if possible change your data structure to something easier. I'll bet that you could parse this easily:

# DATA: Start name: Graq religion: Agnostic Number: 634321 age: 27 hair colour: black height: 73 weight: 123 legs: 2 arms: 2 favorite: jameson detests: bells likes: guinness # DATA: End

buckaduck

Replies are listed 'Best First'.
Re: Re: More Regular Expressions (text data handling)
by graq (Curate) on Dec 04, 2001 at 18:57 UTC

    Well said.

    Unfortunately I cannot provide an actual example.

    So here goes:

    1. I have no control over the incoming data. It will effectively be pasted by someone in a <TEXTAREA>.
    2. The data lines may contain sporadic empty lines, which are to be ignored.
    3. There are useless lines and characters before the useful data starts.
    4. There are useless lines and characters after the useful data ends.
    5. The first unique point at which a line can be identified as being useful, is that it start with the characters 'Number:'.
      This identifier (index) is not at the start of the data.
    6. The data can be split into 4 sections:
      1. TOP
        A set of values with no keys. These are always in the same place relative to each other.
        So, in my previous example, you could safely say $result{Name}='Graq';.
        There is only one value per line.

      2. MIDDLE
        A set of key-value pairs seperated by a colon and space /: /.
        Some lines have 2 key-value pairs on them (never more).
        Keys may contain spaces, values may not.
      3. BOTTOM
        1. A set of key-value pairs seperated by a colon and space.
          One key-value pair per line (see below).
        2. A set of values. These values correspond to key-value pair in (i).
          One value per line (see below).
          NB: The first line of (ii) will be on the same line as the last line of (i), seperated by a space.
      4. END
        A single key-value pair (colon and space seperated), where the value may be missing.

    Note on lines with multiple key-value pairs:

    1. The TOP and MIDDLE never mix.
    2. MIDDLE values may have multiple entries.
    3. MIDDLE and BOTTOM(i) may overlap.
    4. BOTTOM(i) and BOTTOM(ii) always overlap.
    5. BOTTOM(ii) and END never mix.

    Please don't ask why this is :(

    <a href="http://www.graq.co.uk">Graq</a>

    edited by footpad, ~Tue Dec 4 14:42:09 2001 (GMT)