Reputation: 3196
For example I want to match numbers around :
but not if they fall within a parentheses.
str_extract( "2010:2012,mean(2010:2015)", '[0-9:0-9]+')
## 2010:2012 correct
str_extract( "mean(2010:2015),2010:2012", '[0-9:0-9]+')
## 2010:2015 incorrect
How can I check if there parentheses?
Upvotes: 1
Views: 845
Reputation: 785631
First of all [0-9:0-9]+
is not correct. As it matches 1 or more of a digit or colon. So it will match 123
or just :
also.
Correction regex would be:
[0-9]+:[0-9]+
Now if you want to avoid string inside parentheses then use a negative lookahead (assuming all the parentheses are balanced and unescaped):
[0-9]+:[0-9]+(?![^()]*\))
(?![^()]*\))
is a negative lookahead based condition that would fail the match is number:number
is followed by a )
without matching a (
and )
.
Note in r
you will have use double escape it:
'[0-9]+:[0-9]+(?![^()]*\\))'
Upvotes: 2