0

I have a js regex that selects the contents within parenthesis provided there is an equals (=) sign present:

\(([^()]*=[^()]*)\)

This picks up:

GG carriers also responded with higher systolic blood pressure as compared to AA/AG carriers (P=0.008).

But does not pick up:

After 6 weeks of HF diet, circulating ACE concentrations increased by 15% (P=1.6×10(-30))

Is it possible to modify the regex so that it can pick up the string (P=1.6×10(-30))?

Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397
haz
  • 610
  • 7
  • 17
  • @WiktorStribiżew I think it uses the JS flavour – user41805 Feb 10 '17 at 08:09
  • Since it is JS, you need to write some parsing code JS regex engine does not support recursion. – Wiktor Stribiżew Feb 10 '17 at 08:09
  • 1
    Please check [this fiddle](https://jsfiddle.net/gne1pdro/). – Wiktor Stribiżew Feb 10 '17 at 08:36
  • @ Wiktor: firstly thanks for the javascript. I noticed this can be done in php regex: \( (?: [^()]+ | (?R) )*+ \) [http://stackoverflow.com/questions/20569306/how-to-write-a-recursive-regex-that-matches-nested-parentheses] – haz Feb 10 '17 at 14:18
  • `(?:\(.*=.*\))+` This regex should work with javascript. – shove Feb 10 '17 at 14:25
  • @hb: But your question is tagged with JS, surely it can be done in PHP with regex. And in .NET. And in Ruby. I did not test that code, if you think it is worth posting, I will reopen the question. – Wiktor Stribiżew Feb 10 '17 at 14:26
  • a quick survey has shown that recursion is missing from most regex engines with the exception of the ones you named, see [https://en.wikipedia.org/wiki/Comparison_of_regular_expression_engines]. According to wikipedia .NET does not feature recursion – haz Feb 10 '17 at 14:33
  • 1
    I did not say you can do it with *recursion* in .NET regex. I said you can do it with *regex* in .NET. And it is possible since it supports *balanced constructs*. – Wiktor Stribiżew Feb 10 '17 at 14:36
  • @ shove: thanks but it does not seem to work: [https://regex101.com/r/g7VepU/1] – haz Feb 10 '17 at 14:36
  • @ Wiktor: got it, thanks – haz Feb 10 '17 at 14:37
  • 1
    @hb Sorry my fault. I didn't try it in a full text. `(?:\(+[^(]*?=[^)]*?\)+)` this worked for your given example. – shove Feb 10 '17 at 14:46
  • .NET demo - [`\((?:(?=)|[^()]|(?)\(|(?)\))*(?(c)(?!))(?(eq)|(?!))\)`](http://regexstorm.net/tester?p=%5c%28%28%3f%3a%28%3f%3ceq%3e%3d%29%7c%5b%5e%28%29%5d%7c%28%3f%3cc%3e%29%5c%28%7c%28%3f%3c-c%3e%29%5c%29%29*%28%3f%28c%29%28%3f!%29%29%28%3f%28eq%29%7c%28%3f!%29%29%5c%29&i=15%25+%28P%3d1.6%c3%9710%28-30%29%29%2c+tissue+%28P%3d3.8%c3%9710%28-6%29%29.++%28I%2fD%29%2c+revealed++%28GG%29+of++%28P%3d7.5%c3%9710%28-8%29%29+s+%28AA%2fAG%2c+P%3d2%c3%9710%28-6%29%29.++%28P%3d0.008%29.+The++%28MeSyBePo%29+study.). – Wiktor Stribiżew Feb 10 '17 at 15:03
  • @ shove: that works, thank you. please list your solution as an answer so I can accept – haz Feb 11 '17 at 07:19
  • @ Wiktor please reopen the question as shove has answered it, see [https://regex101.com/r/uuk3K8/1] – haz Feb 11 '17 at 07:27
  • [It does not really work](https://regex101.com/r/y9Xzlx/1). – Wiktor Stribiżew Feb 13 '17 at 07:24
  • it works on 2 levels of parentheses when encountering some depictions of an exponent related to p-values. its a fairly specific use case that addresses my application as specifically asked in the question but i appreciate your point – haz Feb 13 '17 at 07:45

0 Answers0