Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As paxdiablo said, you almost certainly don't want to use a regex here. The <code>split</code> suggestion isn't bad; I myself would probably use a parser here&mdash;there's a lot of structure to exploit. The idea here is that you formally specify the syntax of what you have&mdash;sort of like what you gave us, only rigorous. So, for instance: a <code>field</code> is a sequence of non-single-quote characters surrounded by single quotes; a <code>fields</code> is any number of <code>field</code>s separated by white space, a <code>|</code>, and more white space; a <code>tilde</code> is non-right-parenthesis characters surrounded by <code>(~</code> and <code>)</code>; and an <code>expr</code> is a <code>fields</code>, optional whitespace, an optional <code>tilde</code>, a <code>=</code>, optional whitespace, and another <code>fields</code>. How you express this depends on the language you are using. In Haskell, for instance, using the Parsec library, you write each of those parsers as follows:</p> <pre><code>import Text.ParserCombinators.Parsec field :: Parser String field = between (char '\'') (char '\'') $ many (noneOf "'\n") tilde :: Parser String tilde = between (string "(~") (char ')') $ many (noneOf ")\n") fields :: Parser [String] fields = field `sepBy` (try $ spaces &gt;&gt; char '|' &gt;&gt; spaces) expr :: Parser ([String],Maybe String,[String]) expr = do left &lt;- fields spaces opt &lt;- optionMaybe tilde spaces &gt;&gt; char '=' &gt;&gt; spaces right &lt;- fields (char '\n' &gt;&gt; return ()) &lt;|&gt; eof return (left, opt, right) </code></pre> <p>Understanding precisely how this code works isn't really important; the basic idea is to break down what you're parsing, express it in formal rules, and build it back up out of the smaller components. And for something like this, it'll be much cleaner than a regex.</p> <hr> <p>If you <em>really</em> want a regex, here you go (barely tested):</p> <pre><code>^\s*('[^']*'((\s*\|\s*)'[^'\n]*')*)?(\(~[^)\n]*\))?\s*=\s*('[^']*'((\s*\|\s*)'[^'\n]*')*)?\s*$ </code></pre> <p>See why I recommend a parser? When I first wrote this, I got at <em>least</em> two things wrong which I picked up (one per test), and there's probably something else. And I didn't insert capturing groups where you wanted them because I wasn't sure where they'd go. Now yes, I could have made this more readable by inserting comments, etc. And after all, regexen have their uses! However, the point is: this is not one of them. Stick with something better.</p>
    singulars
    1. This table or related slice is empty.
    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. This table or related slice is empty.
    1. This table or related slice is empty.
    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