UPDATED I need to get the characters between braces { }.
For example,
a <- "{a,b}->{v}"
Output :
a,b
and v
UPDATED I need to get the characters between braces { }.
For example,
a <- "{a,b}->{v}"
Output :
a,b
and v
You can use stingr's str_extract_all
In the following expression (?<=\\{)
is used to find opening curly braces, (?=\\})
is used to detect closed braces and .+?
is used to extract text in between. Hence, the final expression would become (?<=\\{).+?(?=\\})
This will return a list()
str_extract_all(a, "(?<=\\{).+?(?=\\})")[[1]]
Please follow another example performed by me:
> a <- "{a,b}->{v}{d}{c}{67}"
> str_extract_all(a, "(?<=\\{).+?(?=\\})")[[1]]
[1] "a,b" "v" "d" "c" "67"
If you need to match strings in between curly braces excluding the curly braces, you may use
a <- "{a,b}->{v}"
stringr::str_extract_all(a, "(?<=\\{)[^{}]+(?=\\})") # With stringr library
# => [1] "a,b" "v"
regmatches(a, gregexpr("(?<=\\{)[^{}]+(?=\\})", a, perl=TRUE)) # Base R approach #1
# => [1] "a,b" "v"
regmatches(a, gregexpr("\\{\\K[^{}]+(?=\\})", a, perl=TRUE)) # Base R approach #2
# => [1] "a,b" "v"
See the regex #1 demo. Details:
(?<=\{)
- a positive lookbehind that requires a {
immediately to the left of the current location[^{}]+
- 1 or more (due to the +
quantifier) chars other than {
and }
(the [^...]
is a negated bracket expression in the TRE regex that is used by default in base R regex functions (or a negated character class in NFA regex, as is used in the ICU regexps in stringr package)(?=\})
- a positive lookahead that requires a }
immediately to the left of the current location\{\K
means that after matching and consuming {
, the text matched is discarded from the match value, so the {
does not land in the results. See Keep The Text Matched So Far out of The Overall Regex Match for more details.To match strings inside non-nested curly braces including the curly braces, you may use
a <- "{a,b}->{v}"
stringr::str_extract_all(a, "\\{[^{}]*\\}") # With stringr library
regmatches(a, gregexpr("\\{[^{}]*}", a)) # Base R approach
# => [1] "{a,b}" "{v}"
See the regex
Here, \{[^{}]*\}
matches all substrings starting with {
, then 0+ chars other than {
and }
(with [^{}]*
) and then ending with }
.
See the R demo online.
Sorry that I'm answering my own questions but
j <- "{a,b}->{v}"
unlist(strsplit(j, split="[{}]"))
Apparently for braces and brackets, we have to put it inside []
below is code
var a = "{a,b} xyz {v}";
a = a.split(" ");
a[0] //outupt {a,b}
a[2] //output {v}
Here is my solution
library(stringr)
a <- "{a,b}->{v}"
betw_curly <- function(a) {
str_sub(a,
str_locate_all(a, '\\{')[[1]][,1]+1,
str_locate_all(a, '\\}')[[1]][,1]-1)
}
betw_curly(a)
[1] "a,b" "v"
The function tools::delimMatch()
is designed for just this purpose.
tx <- '\\caption{Groups are \\code{ctl} and \\code{trt}}.\label{fig:gps}'
tools::delimMatch(tx, delim = c("{", "}"))
## [1] 9
## attr(,"match.length")
## [1] 38
substring(tx,9,9+38-1)
## "{Groups are \\code{ctl} and \\code{trt}}"
Note that a second match ({fig:gps}
) was not captured.