我希望提取带有任何小数的数字(小数点两侧至少有一个数字),但不是后跟百分比的模式.因此,我相信我需要一个负面的前瞻(所以它可以看到数字后面是否跟一个百分号).
I wish to extract numbers with any decimals (at least one number both sides of the decimal), but not patterns followed by percentages. Therefore, I believe I need a negative lookahead (so it can see if the number is followed by a percentage sign).
为了清楚起见,我想提取 "123.123",但不想提取 "123.123%"
For clarity, I would want to extract "123.123", but would not like to extract "123.123%"
我尝试了十几种语法安排,但找不到有效的安排.这成功地提取了十进制模式.
I have tried a dozen syntax arrangements but cannot find the one that works. This successfully extracts the decimal pattern.
c("123.123%", "123.123") %>% str_extract_all(., "\\d+\\.\\d+")但我想修改它以仅返回第二个项目(因为第一个包含百分号.
But I want to adapt it to return the second item only (since the first contains a percentage sign.
我尝试了以下各种组合:
I have tried various combinations of the following:
c("123.123%", "123.123") %>% str_extract_all(., "\\d+\\.\\d+(!?=%)") c("123.123%", "123.123") %>% str_extract_all(., "\\d+\\.\\d+[!?%]") c("123.123%", "123.123") %>% str_extract_all(., "\\d+\\.\\d+!?%") c("123.123%", "123.123") %>% str_extract_all(., "\\d+\\.\\d+!?\\%") c("123.123%", "123.123") %>% str_extract_all(., "\\d+\\.\\d+(!?=\\%)") # etc 推荐答案您可以使用
"\\d+\\.\\d++(?!%)"\d++(?!%) 部分全部匹配 1 个或多个数字,并且执行 (?!%) 负前瞻在所有这些数字都匹配之后,如果后面有 % 则匹配失败.
The \d++(?!%) part matches 1 or more digits possessively and the (?!%) negative lookahead is executed once after all those digits are matched and fails the match if there is a % after them.
同样可以在没有所有格量词的情况下写成 "\\d+\\.\\d+(?![%\\d])",其中 (?![%\\d]) 如果当前位置右侧有一个数字,则匹配也会失败.
The same can be written without a possessive quantifier as "\\d+\\.\\d+(?![%\\d])", where the (?![%\\d]) will also fail the match if there is a digit immediately to the right of the current location.
R 演示:
> library(stringr) > c("123.123%", "123.123") %>% str_extract_all(., "\\d+\\.\\d++(?!%)") [[1]] character(0) [[2]] [1] "123.123"更多推荐
正则表达式中的负前瞻以排除 R 中的百分比 (%)
发布评论