如何在没有\ r \ n的情况下从下面的文本中提取地址(39 / 4B ....... 700025)?
Text<-"From :\r\nName : NAMITA ROY\r\nAddress : 39/4B\r\n GOPALNAGAR ROAD\r\n ALIPORE\r\n KOLKATA,WEST BENGAL\r\n 700027\r\nEntity \r\nName : SWARNABARSA PROJECTS PRIVATE LIMITED\r\nAddress : 90A\r\n RAJ SEKHAR BOSE SARANI, FLAT NO.1D, 1ST FLOOR\r\n KOLKATA,WEST BENGAL\r\n INDIA - 700025\r\nFull Particulars of Remittance\r\nService Type: eFiling\r\n"
答案 0 :(得分:3)
尝试
trimws(unlist(strsplit(unlist(strsplit(gsub("\r\n|\\s+", " ", Text), ":"))[4], "Entity Name"))[1])
# [1] "39/4B GOPALNAGAR ROAD ALIPORE KOLKATA,WEST BENGAL 700027"
答案 1 :(得分:2)
我的代码从AFTER&#34;地址:&#34; TILL并包括6位数(ZIP)
strsplit(Text,"Name(\\s+)?:")[[1]][-1] %>% list %>% lapply(function(x)gsub(x=x,pattern="[\\s\\S]*?Address\\s+:([\\s\\S]*?\\d{6})[\\s\\S]*?$",replacement="\\1",perl=T)) %>%
lapply(function(x)gsub(x=x,pattern="\\r|\\n",replacement="",perl=T)) %>% lapply(function(x)trimws(gsub(x=x,pattern="\\s+",replacement=" ",perl=T)))
结果:
[[1]]
[1] "39/4B GOPALNAGAR ROAD ALIPORE KOLKATA,WEST BENGAL 700027"
[2] "90A RAJ SEKHAR BOSE SARANI, FLAT NO.1D, 1ST FLOOR KOLKATA,WEST BENGAL INDIA - 700025"
答案 2 :(得分:1)
尝试这种方式:
Text<-"From :\r\nName : NAMITA ROY\r\nAddress : 39/4B\r\n GOPALNAGAR ROAD\r\n ALIPORE\r\n KOLKATA,WEST BENGAL\r\n 700027\r\nEntity \r\nName : SWARNABARSA PROJECTS PRIVATE LIMITED\r\nAddress : 90A\r\n RAJ SEKHAR BOSE SARANI, FLAT NO.1D, 1ST FLOOR\r\n KOLKATA,WEST BENGAL\r\n INDIA - 700025\r\nFull Particulars of Remittance\r\nService Type: eFiling\r\n"
#Remove redundant spaces
library(stringr)
Text<-gsub("\\s+", " ", str_trim(Text))
address_dirty<-unlist(strsplit(Text,split = "Address : "))[2]
posiz<-regexpr("[0-9]{6,}",address_dirty) #Find ZIP Code posizion
address<-substr(address_dirty,1,posiz[1]+5)
address
[1] "39/4B GOPALNAGAR ROAD ALIPORE KOLKATA,WEST BENGAL 700027"
代码提取地址beetwen strings Address
和邮政编码。