To get the text inside the last set of [...]
brackets, you may use a sub
with the following pattern:
".*\\[([^][]+)].*"
The pattern matches:
.*
- any 0+ chars greedily, as many as possible, up to the last occurrence of the subsequent subpatterns
\\[
- a literal [
(must be escaped outside of the bracket expression)
([^][]+)
- Group 1 (later referred to with \1
) matching 1 or more chars other than ]
and [
]
- a literal ]
(no need escaping it outside of a bracket expression
.*
- the rest of the string.
R online demo:
x <- c("[ghjg6] [fdg5] [113gi4lki] great work", "[xzswedc: acf] [xzt8] [111eerrh5]", "[asd2] [1] [113vu17hg 115er5lgr 112cgnmbh ] get out", "Some text with no brackets")
df <- data.frame(x)
df$x = sub(".*\\[([^][]+)].*", "\\1", df$x)
df
Output:
x
1 113gi4lki
2 111eerrh5
3 113vu17hg 115er5lgr 112cgnmbh
4 Some text with no brackets
If you want to remove the entries with no [...]
(like the last one in my test set), use
df$x = sub(".*\\[([^][]+)].*|.*", "\\1", df$x)
See another online R demo.