Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm working with a character vector of 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 ) so I can get the desired result resembling:

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

Attempts

1 - Groups,

I was thinking of capturing first and second group, on the lines of the syntax:

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

but it doesn't seem to work, (regex101).

2 - Punctuation

The code: [[:punct:]] will capture all punctuation regex101.

Match

3 - Groups again

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


The problem can be easily solved by applying gsub twice or making use of the multigsub available in the qdap package but I'm interested in solving this via one expression, is possible.

like image 237
Konrad Avatar asked Oct 19 '22 17:10

Konrad


1 Answers

You could try using lookaheads and lookbehinds 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"
like image 59
Thomas Avatar answered Nov 17 '22 10:11

Thomas