正则表达式 – R从模式结尾提取子字符串直到第一次出现字符
发布时间:2020-12-14 05:56:02 所属栏目:百科 来源:网络整理
导读:努力争取这场比赛并在R gsub取代工作仍然没有成功. 我试图在一个字符串中匹配模式“Reason:”,并在此模式之后接触所有内容,直到第一次出现一个点(.) 例如: Offer Disposition. MSISDN: 7183067962. Offer: . Disposition: DECLINED. Reason: Not intereste
努力争取这场比赛并在R gsub取代工作仍然没有成功.
我试图在一个字符串中匹配模式“Reason:”,并在此模式之后接触所有内容,直到第一次出现一个点(.) 例如: Offer Disposition. MSISDN: 7183067962. Offer: . Disposition: DECLINED. Reason: Not interested. ChannelID: CARE. 会回来“不感兴趣” 解决方法
这是一个解决方案:
s <- "Offer Disposition. MSISDN: 7183067962. Offer: . Disposition: DECLINED. Reason: Not interested. ChannelID: CARE." sub(".*Reason: (.*?)..*","1",s) # [1] "Not interested" 更新(发表评论): 如果你还有与模式不匹配的字符串,我建议使用regexpr而不是sub: s2 <- c("no match example","Offer Disposition. MSISDN: 7183067962. Offer: . Disposition: DECLINED. Reason: Not interested. ChannelID: CARE.") match <- regexpr("(?<=Reason: ).*?(?=.)",s2,perl = TRUE) ifelse(match == -1,NA,regmatches(s2,match)) # [1] NA "Not interested. ChannelID: CARE" 对于第二个示例,您可以使用以下正则表达式: s3 <- "Delete Payment Arrangement of type Proof of Payment for BAN : 907295267 on date 02/01/2014,from reason PAERR." # a) sub(".*type (.*?) for.*",s3) # [1] "Proof of Payment" # b) match <- regexpr("(?<=type ).*?(?= for)",s3,regmatches(s3,match)) # [1] "Proof of Payment" (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |