Matching the last and first brackets in gsub / r and leaving the remaining content intact

I work with a character symbol in the following format:

[-0.2122,-0.1213) [-0.2750,-0.2122) [-0.1213,-0.0222) [-0.1213,-0.0222) 

I would like to remove [ and ) to get the desired result, similar to:

 -0.2122,-0.1213 -0.2750,-0.2122 -0.1213,-0.0222 -0.1213,-0.0222 

Attempts

1 - Groups

I was thinking about capturing the first and second groups in the syntax lines:

 [[^\[{1}(?![[:digit:]])\){1} 

but it does not work, ( regex101 ).

2 - Punctuation

Code: [[:punct:]] will capture all regex101 punctuation marks.

Match

3 - groups again

Then I tried to match two groups: (\[)(\)) , but again, the lack of regex101 .


The problem can be easily solved by using gsub twice or using multigsub available in the qdap package qdap but I'm interested in solving this through one expression, maybe.

+5
source share
1 answer

You can try using lookaheads and lookbehind in Perl-style regular expressions.

 x <- scan(what = character(), text = "[-0.2122,-0.1213) [-0.2750,-0.2122) [-0.1213,-0.0222) [-0.1213,-0.0222)") regmatches(x, regexpr("(?<=\\[).+(?=\\))", x, perl = TRUE)) # [1] "-0.2122,-0.1213" "-0.2750,-0.2122" "-0.1213,-0.0222" "-0.1213,-0.0222" 
+3
source

Source: https://habr.com/ru/post/1241029/


All Articles