Reputation: 43
String:
50-60*70/50+(1+7-(9+51))+5*9/10
Want:
9 +51
Tried:
(?:\(|\G[\+\*\/-])\K(\d+)
Result:
1 7 9 51
Upvotes: 3
Views: 87
Reputation: 626729
It seems you are using a PCRE regex to get the numbers that can be preceded with math operators inside (...)
that has no inner ()
.
Use
(?:\(|(?!^)\G)\K[+*\/-]?\d+(?=[^()]*\))
See regex demo
Explanation:
(?:\(|(?!^)\G)\K
- Find (
or the end of the previous successful match, and omit the match text currently stored in the memory (with \K
)[+*\/-]?
- one or zero operators\d+
- one or more digits(?=[^()]*\))
- but only if followed by zero or more characters other than (
and )
up to the closing )
.Note that in your regex, you omit the math operators since they are before \K
and you find the numbers after the math operators or (
(and after the end of each successful match, that is why you get 1
, 7
, 9
, 51
.
Upvotes: 1