title: Regex on spark dataframe column
body: <p>Have a dataframe, which has a query as a value in one of the column, I am trying to extract the value between one/two parentheses in the first group using regex.</p>
  
  <p>My Input the regex is:</p>
  
  <pre><code>select nvl(sum(field1),0), field2, field3 from tableName1 where partition_date='2018-03-13'
  </code></pre>
  
  <p>Output should be:</p>
  
  <pre><code>field1
  </code></pre>
  
  <p>Spark Code what I used to extract the value is:</p>
  
  <pre><code>target_query = "select nvl(sum(field1),0), field2, field3 from tableName1 where partition_date='2018-03-13'"
  
  val df1 = df.withColumn("Extract_field", regexp_extract(df("target_query"),"(?&lt;=\\().*?(?=\\))",0))
  </code></pre>
  
  <p>But I'm getting </p>
  
  <pre><code>sum(field1
  </code></pre>
  
  <p>Please let me know how to extract only the <code>field1</code>. I may get <code>sum(field1)</code> instead of <code>nvl(sum(field1),0)</code> as well. Is it possible to use same regex to handle both the cases?</p>