title: Java Regex expression to match content within parenthesis
body: <p>I am currently working on a calculator project. I am working on the lexer. My intent is to create a calculator that can take inputs such as </p>
  
  <pre><code>5+3
  
  4*8
  
  3*(4+2)
  </code></pre>
  
  <p>and return an answer. I am attempting to create regular expressions that can split these up into tokens. I want to split expressions in parenthesis into a single token that will later be solved recursively. I am able to match my numbers and operators correctly, but I can't get it to work for my parenthesis expressions. Here is my current regular expression:</p>
  
  <pre><code>String tokenPatterns ="(?&lt;NUMBER&gt;-?[0-9]+\\.[0-9]+|-?[0-9]+)" //match any integer or decimal number
              + "|(?&lt;OPERATOR&gt;\\+|-|/|\\*)" //match operators     
              + "|(?&lt;UNIT&gt;in|pt)" //match a few units
              + "|(?&lt;WHITESPACE&gt;\\S+)" //match whitespace - is just discarded later, most likely unnecessary
              + "|(?&lt;PAREN&gt;\\([^\\n]*\\))"; //match any character series in parentheses, excluding newline
  </code></pre>
  
  <p>My testing has been successful for all of these except the parenthesis one. My understanding is that what I have written for parenthesis should match:</p>
  
  <p>open parenthesis followed by any number of any characters excluding newline followed by close parenthesis</p>
  
  <p>but when I give it the input of -34*(5+2), it only matches -34 and /.</p>
  
  <p>Does anybody have an explanation or tip? Let me know if you need more context or clarification, and I will do my best to respond quickly. Thanks!</p>