简体   繁体   中英

R regex - removing pattern from ends

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.

https://regex101.com/r/uF4oY4/66

Use as

gsub("(^N{1,}|N{1,}$)","",x)

https://regex101.com/r/uF4oY4/69

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM