匹配字符串,但前提是没有其他字符串 [英] Match string, but only if not preceded by other string

查看:88
本文介绍了匹配字符串,但前提是没有其他字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有字符串向量:

  v = c(SPX.Close,AAPL.Low, Lo,LowPrice,PriceLow,low)

如何编写正则表达式会匹配所有类似短语low的字符串?

grep(lo,v,ignore.case = T)#1 2 3 4 5 6 7



这也匹配第一个字符串,我不想要。



如何匹配 lo 仅在前面加字母 c

解决方案

R使用PCRE引擎,它支持倒序。这样做:

  grep((?<!c)lo,subject,perl = TRUE,value = TRUE, ignore.case = TRUE); 

负面后视(?断言当前位置之前的内容不是 c



选项2:检查资本Letter,打开不区分大小写内联



考虑到您的输入,更常用的选项是断言 lo 前面没有大写字母:

  grep((?<![AZ])(? i)lo,subject,perl = TRUE,value = TRUE); 

对于这个选项,我们使用内联修饰符(?i) code>打开不区分大小写,但是只有在我们检查过没有大写字母在我们的位置之前。

参考




Say I have vector of strings:

v = c("SPX.Close", "AAPL.Low", "Lo", "LowPrice", "PriceLow", "low")

How to write regex that would match all strings resembling phrase "low"?

grep("lo", v, ignore.case=T) # 1 2 3 4 5 6 7

This matches the first string too, which I don't want.

How to match lo only if not preceded by letter c ?

解决方案

Negative Lookbehind (PCRE in R)

R uses the PCRE engine, which supports lookbehind. Do this:

grep("(?<!c)lo", subject, perl=TRUE, value=TRUE, ignore.case=TRUE);

The negative lookbehind (?<!c) asserts that what precedes the current position is not a c

Option 2: Check for Capital Letter, Turn On Case-Insensitivity Inline

Given your input, a more general option would be to assert that lo is not preceded by a capital letter:

grep("(?<![A-Z])(?i)lo", subject, perl=TRUE, value=TRUE);

For this option, we use the inline modifier (?i) to turn on case-insensitivity, but only after we have checked that no capital letters precede our position.

Reference

这篇关于匹配字符串,但前提是没有其他字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆