Reputation: 521
I have a string like this:
myString <- "[0.15][4577896]blahblahblahblahwhatever"
I need to extract the number between second brackets.
Currently I am trying to use this:
str_extract(myString, "\\]\\[(\\d+)")
But this gives me ][4577896
My desired result would be: 4577896
How could I achieve this?
Upvotes: 0
Views: 74
Reputation: 886938
An option using str_extract
library(stringr)
str_extract(myString, "(?<=.\\[)([0-9]+)")
#[1] "4577896"
Upvotes: 1
Reputation: 388807
Here is another version with minimal or no regex
qdapRegex::ex_between_multiple(myString, "[", "]")[[2]]
#[1] "4577896"
It extracts all the substring between [
and ]
and we select the value between second bracket. You can convert it into numeric or integer if needed.
Upvotes: 2
Reputation: 2467
With no need of look behinds
gsub(".*\\[(\\d+).*","\\1",myString)
[1] "4577896"
Upvotes: 3
Reputation: 735
You can try this .(?<=\]\[)(\d+)
This is a demo.https://regex101.com/r/fvHW05/1
Upvotes: 2
Reputation: 43169
You may use
^(?:[^\[\]]*\[[^\[\]]+\])[^\]\[]*\[([^\]\[]+).+
And replace this with the first captured group using gsub
, see a demo on regex101.com.
In base R
:
myString <- "[0.15][4577896]blahblahblahblahwhatever"
pattern <- "^(?:[^\\[\\]]*\\[[^\\[\\]]+\\])[^\\]\\[]*\\[([^\\]\\[]+).+"
gsub(pattern, "\\1", myString, perl = T)
# [1] "4577896"
Upvotes: 1