[英]Regex for matching a phrase followed by certain numbers followed by certain string
Another regex problem I can't solve.另一个我无法解决的正则表达式问题。 :/
:/
I have the following input vector:我有以下输入向量:
x <- c("test", paste0(rep(paste0("Q18r", c(1:14, 997)), each = 2), c("c1", "c2")))
I now want to do the following match:我现在想做以下比赛:
The problem is that I can't figure out how to add the "c1" at the end, ie I get it working at least for the first two matches with:问题是我无法弄清楚如何在末尾添加“c1”,即我至少在前两场比赛中让它工作:
stringr::str_subset(x, "Q18r(?=[1-3]|1[1-2])|(997)")
I tried a lot of different c1 matches, ie (?=c1)
, only c1
, (c1)
etc. but nothing works.我尝试了很多不同的 c1 匹配,即
(?=c1)
,只有c1
, (c1)
等,但没有任何效果。
Any ideas?有任何想法吗?
You can use您可以使用
stringr::str_subset(x, "Q18r(?:[1-3]|1[1-2]|997)c1")
[1] "Q18r1c1" "Q18r2c1" "Q18r3c1" "Q18r11c1" "Q18r12c1" "Q18r997c1"
Regex details :正则表达式详细信息:
Q18r
- a fixed Q18r
string Q18r
- 固定的Q18r
字符串(?:[1-3]|1[1-2]|997)
- a non-capturing group matching 1
, 2
, 3
, 11
, 12
or 997
(?:[1-3]|1[1-2]|997)
- 匹配1
、 2
、 3
、 11
、 12
或997
的非捕获组c1
- a c1
string. c1
- 一个c1
字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.