正则表达式匹配准确的字母数 [英] Regex match exact number of letters
问题描述
假设我想找到字母e"恰好出现两次的所有单词.当我定义这个模式时:
Let's say I want to find all words in which letter "e" appears exactly two times. When I define this pattern:
pattern1 <- "e.*e"
grep(pattern1, stringr::words, value = T)
RegEx 也匹配诸如therefore"之类的词,因为e"也(至少)出现两次.关键是,我不希望我的模式是至少",我希望它是正好 n 次".
RegEx also matches words such as "therefore", because "e" appears (at least) two times as well. The point is, I don't want my pattern to be "at least", I want it to be "exactly n times".
这种模式...
pattern2 <- "e{2}"
...找到带有两个字母e"的单词,但前提是它们一个接一个出现(feel"、agre"等).我想将这两种模式结合起来,以找到所有字母e"不一定连续出现的确切次数的所有单词.
...finds words with two letter "e", but only if they appear one after each other ("feel", "agre" etc). I'd like to combines these two patterns to find all words with exact number of not necessarily consecutive appearances of a letter "e".
推荐答案
您可以使用:
^(?:[^e]*e){2}[^e]*$
查看正则表达式演示.(?:...)
是一个非捕获组,它允许量化子模式的序列,因此可以轻松调整以匹配 3、4 或更多个特定序列一个字符串.
See the regex demo. The (?:...)
is a non-capturing group that allows quantifying a sequence of subpatterns and is thus easily adjustable to match 3, 4 or more specific sequences in a string.
详情
^
- 字符串开始(?:[^e]*e){2}
- 2 次出现[^e]*
- 除了e
之外的任何 0+ 个字符e
-e
^
- start of string(?:[^e]*e){2}
- 2 occurrences of[^e]*
- any 0+ chars other thane
e
- ane
请参阅下面的 R 演示:
x <- c("feel", "agre", "degree") rx <- "^(?:[^e]*e){2}[^e]*$" grep(rx, x, value = TRUE) ## => [1] "feel"
请注意,使用
value = TRUE
代替value = T
更安全,因为T
可能会在上面的代码中重新定义.Note that instead of
value = T
it is safer to usevalue = TRUE
asT
might be redefined in the code above.这篇关于正则表达式匹配准确的字母数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!