Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>People don't seem to get the fact that they don't have to use REs (or SQL, but that's another issue :-) for every task, especially those with procedural code is cleaner.</p> <p>If you're limiting yourself to using REs, I think that's a lack of vision.</p> <p>I would simply process the string, token by token, where a token is one of:</p> <ul> <li>a non-delimiter.</li> <li>a column delimiter.</li> <li>a row delimiter.</li> </ul> <p>Start with an empty column list, then extract (using indexOf/substring stuff) up to the first next row/column delimiter, adding that text to the column list.</p> <p>If the delimiter is column, keep going.</p> <p>If the delimiter is row, check the number of columns and process the list as required.</p> <p>If there's no final row delimiter and the column list is non-empty, then the format was invalid.</p> <p>Sorry if you were really after an RE method but I don't believe it's required (or even desirable) here.</p> <p>Pseudo-code (only a first cut, may be slightly buggy) follows:</p> <pre><code>def processStr(s): if not s.endsWith ("|ROW-DELIM|"): error "Invalid format" columnList = [] while not s.equals (""): nextRowDelim = s.indexOf ("|ROW-DELIM|") nextColDelim = s.indexOf ("|COL-DELIM|") if nextColDelim == NotFound: nextColDelim = nextRowDelim + 1 nextDelim = minimumOf (nextRowDelim,nextColDelim) columnList.add (s.substring (0, nextDelim)) s = s.substring (nextDelim) if nextDelim == nextRowDelim: s = s.substring (length ("|ROW-DELIM|")) processColumns (columnList) columnList = [] else: s = s.substring (length ("|COL-DELIM|")) </code></pre> <p>You could easily add code to check the correct number of columns in this code, or in <code>processColumns()</code>, if that was your desire.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload