I want to extract "SUBSTRING" with sub() from the following string:
attribute <- "S4Q7b1_t1_r1: SUBSTRING: some explanation: some explanation - ..."
I used the following code, but unfortunately it didn't work:
sub(".*: (.*) : .*", "\\1", attribute)
Does anyone know an answer for that?
You may use
sub("^[^:]*: ([^:]*).*", "\\1", attribute)
See the regex demo
You need to rely on negated character classes, [^:]
that matches any char but :
, since .*
matches greedily any 0 or more chars. Also, your pattern contains a space before :
and it is missing in the string.
Details
^
- start of string [^:]*
- any 0+ chars other than :
:
- a colon with a space - ([^:]*)
- Capturing group 1 ( \\1
refers to this value): any 0+ chars other than :
.*
- the rest of the string. R Demo :
attribute <- "S4Q7b1_t1_r1: SUBSTRING: some explanation: some explanation - ..."
sub("^[^:]*: ([^:]*).*", "\\1", attribute)
## => [1] "SUBSTRING"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.