0
我想查找一些模式後的字符串。我的代碼似乎工作,但我無法完成這項工作。R使用正則表達式,與多種模式
這裏有一個例子:
pattern <- c("Iligan", "Cabeseria 25|Sta. Lucia", "Capitol", "Osmeña",
"Nowhere", "Aglayan")
# I want to match the string just after each pattern. For example I'm going to
# match City just after Iligan.
target <-c("Iligan City", "Sta. Lucia, Ozamiz City", " Oroquieta City",
"Osmeña St. Dipolog City", "Lucia St., Zamboanga City",
"Aglayan str, Oroquieta City", "Gingoog City", "Capitol br., Ozamiz City",
"Dumaguete City", "Poblacion, Misamis")
#The matches seems to work fine
(matches <- sapply(pattern,FUN=function(x){regexpr(paste0("
(?<=\\b",x,"\\b ",")","[\\w-*\\.]*"),target,perl=T)}))
print (matches)
#But I cannot get the results. I would need use the column of each matrix
#at a time
villain <- lapply(matches,FUN = function(x)(regmatches(target,x)))
你有沒有解決這個問題。
unpdate 1
對於被精確這裏起見是所需的輸出。
results <- c("City", "St.", "br.")
#[1] "City" "St." "br."
預期產量是多少?只是匹配的字符串列表(沒有'NA's)? – hrbrmstr 2014-10-03 11:11:41
什麼是unpdate? – amonk 2017-07-19 12:20:37