正则表达式中的负前瞻以排除 R 中的百分比 (%)

编程入门 行业动态 更新时间:2024-10-23 01:57:00
本文介绍了正则表达式中的负前瞻以排除 R 中的百分比 (%)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我希望提取带有任何小数的数字(小数点两侧至少有一个数字),但不是后跟百分比的模式.因此,我相信我需要一个负面的前瞻(所以它可以看到数字后面是否跟一个百分号).

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 中的百分比 (%)

本文发布于:2023-11-06 06:33:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1562949.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:百分比   正则表达式

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!