从字符串中删除数字,除非之前有一个字母

时间:2018-01-12 03:19:32

标签: r regex

我有这个字符串:

     str <-c ("Street 21, h0use blu3 number 23A", "th3 hosp1tal on 7A Street with 12A")
str <- gsub ("\\ b (?! Street) \\ s [0-9] | [0-9]", "", str, perl = T)

这个结果:

"Street, huse blu numberA" "the hosptal onA Street withA"

我试图消除所有数字,除非他们指的是街道,我想要的结果如下:

"Street 21, huse blu number 23A" "th hosptal on 7A Street 12A"

1 个答案:

答案 0 :(得分:1)

使用lookbehind检查数字前面是否有一个字母:

str<-c("Street 21, h0use blu3 number 23A", "th3 hosp1tal on 7A Street with 12A")
gsub("(?<=[A-Za-z])\\d+", "", str, perl=TRUE)

[1] "Street 21, huse blu number 23A"   "th hosptal on 7A Street with 12A"

Demo