Suppose I have a string that looks like so:
x <- "NNNNAAAJNFHANFFADN"
How would I only remove the N's from the ends to get:
"AAAJNFHANFFAD"
Just match and remove the N
's which exists at the start or at the end through gsub
.
gsub("^N+|N+$", "", x)
^N+
matches one or more N
's which exists at the start. |
Alternation operator. N+$
Matches one or more N's which exists at the end. Example:
> x <- "NNNNAAAJNFHANFFADN"
> gsub("^N+|N+$", "", x)
[1] "AAAJNFHANFFAD"
gsub("^N*([A-Z]*?)N*$", "\\1", x)
You can use \\1
to backreference here.See demo.
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.