gpt4 book ai didi

r - 为什么这个正则表达式模式匹配这个字符串?

转载 作者:行者123 更新时间:2023-12-04 08:56:30 25 4
gpt4 key购买 nike

图案:

"d`?(?!([\\s]*<-))"
字符串:
"d` <-"
根据R,这是一场比赛:
> grepl("d`?(?!([\\s]*<-))", "d` <-", perl = TRUE)
[1] TRUE
这对我来说没有意义,因为 d匹配,(0 或 1)反引号匹配。但是 " <-"不应该是一场比赛吗?

最佳答案

原因是回溯,如 Wiktor Stribizew's comment 中所述。 .
将可选的反引号添加到前瞻中并将前瞻移动到 d 之后:

d(?!`?(\s*<-))`?
proof
说明
  [a-z]                    any character of: 'a' to 'z'
--------------------------------------------------------------------------------
\d{2,4} digits (0-9) (between 2 and 4 times
(matching the most amount possible))
--------------------------------------------------------------------------------
(?: group, but do not capture (optional
(matching the most amount possible)):
--------------------------------------------------------------------------------
- '-'
--------------------------------------------------------------------------------
[a-z] any character of: 'a' to 'z'
--------------------------------------------------------------------------------
\d{2,4} digits (0-9) (between 2 and 4 times
(matching the most amount possible))
--------------------------------------------------------------------------------
)? end of grouping
--------------------------------------------------------------------------------
(?: group, but do not capture (optional
(matching the most amount possible)):
--------------------------------------------------------------------------------
- '-'
--------------------------------------------------------------------------------
[a-z] any character of: 'a' to 'z'
--------------------------------------------------------------------------------
)? end of grouping

关于r - 为什么这个正则表达式模式匹配这个字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63801817/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com