Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I specify the priority of a match pattern in a Regex?
    primarykey
    data
    text
    <p>I'm writing a function-parsing engine which uses regular expressions to separate the individual terms (defined as a constant or a variable followed (optionally) by an operator). It's working great, except when I have grouped terms within other grouped terms. Here's the code I'm using:</p> <pre><code>//This matches an opening delimiter Regex openers = new Regex("[\\[\\{\\(]"); //This matches a closing delimiter Regex closers = new Regex("[\\]\\}\\)]"); //This matches the name of a variable (\w+) or a constant numeric value (\d+(\.\d+)?) Regex VariableOrConstant = new Regex("((\\d+(\\.\\d+)?)|\\w+)" + FunctionTerm.opRegex + "?"); //This matches the binary operators +, *, -, or / Regex ops = new Regex("[\\*\\+\\-/]"); //This compound Regex finds a single variable or constant term (including a proceeding operator, //if any) OR a group containing multiple terms (and their proceeding operators, if any) //and a proceeding operator, if any. //Matches that match this second pattern need to be added to the function as sub-functions, //not as individual terms, to ensure the correct evalutation order with parentheses. Regex splitter = new Regex( openers + "(" + VariableOrConstant + ")+" + closers + ops + "?" + "|" + "(" + VariableOrConstant + ")" + ops + "?"); </code></pre> <p>When "splitter" is matched against the string "4/(2*X*[2+1])", the matches' values are "4/", "2*", "X*", "2+", and "1", completely ignoring all of the delimiting parentheses and braces. I believe this is because the second half of the "splitter" Regex (the part after the "|") is being matched and overriding the other part of the pattern. This is bad- I want grouped expressions to take precedence over single terms. Does anyone know how I can do this? I looked into using positive/negative lookaheads and lookbehinds, but I'm honestly not sure how to use those, or what they're even for, for that matter, and I can't find any relevant examples... Thanks in advance.</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.
 

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