[英]Replicating regex expression using Rebus package in R
I would like to create a pattern for the following text string using the rebus
package in R.我想使用 R 中的
rebus
package 为以下文本字符串创建一个模式。
My attempts are below, but I am not able to remove the square brackets and return the same pattern using str_view()
.我的尝试如下,但我无法删除方括号并使用
str_view()
返回相同的模式。 Is there perhaps a tool / function that can replicate regex expressions using the rebus package?是否有一个工具 / function 可以使用 rebus package 复制正则表达式? Rebus is a lot easier to read and makes sense when sharing code with someone that might not be familiar with regex.
当与可能不熟悉正则表达式的人共享代码时,Rebus 更容易阅读并且有意义。
Pattern with regex:带有正则表达式的模式:
pattern = "http.*for-sale.*5857"
I am trying to replicate this with the rebus package:我正在尝试使用 rebus package 来复制它:
pattern_rebus = "http" %R% zero_or_more(ANY_CHAR) %R% "for-sale" %R% zero_or_more(ANY_CHAR) %R% "5857"
as.regex(pattern_rebus)
<regex> http[.]*for-sale[.]*5857
There is a bug in rebus
, it wraps all the repeated ( one_or_more
or zero_or_more
) chars with [
and ]
, a character class. rebus
中有一个错误,它用[
和]
包装了所有重复的( one_or_more
或zero_or_more
)字符,即字符 class。 That is why .*
should be added manually.这就是为什么
.*
应该手动添加的原因。
pattern_rebus = "http" %R% ".*" %R% "for-sale" %R% ".*5857"
as.regex(pattern_rebus)
## => <regex> http.*for-sale.*5857
However, you may use a workaround, [\s\S]
instead of a .
但是,您可以使用变通方法
[\s\S]
而不是.
will match any chars if you use a PCRE regex (with base R regex functions) or ICU regex (with stringr regex functions):如果您使用 PCRE 正则表达式(带有基本 R 正则表达式函数)或 ICU 正则表达式(带有stringr正则表达式函数),将匹配任何字符:
pattern_rebus = "http" %R% zero_or_more(char_class(WRD, NOT_WRD)) %R% "for-sale" %R% zero_or_more(char_class(WRD, NOT_WRD)) %R% "5857"
as.regex(pattern_rebus)
## => <regex> http[\w\W]*for-sale[\w\W]*5857
Or, if you want to match any char but CR and LF:或者,如果您想匹配除 CR 和 LF 之外的任何字符:
pattern_rebus = "http" %R% zero_or_more(negated_char_class("\\r\\n")) %R% "for-sale" %R% zero_or_more(negated_char_class("\\r\\n")) %R% "5857"
as.regex(pattern_rebus)
## => <regex> http[^\r\n]*for-sale[^\r\n]*5857
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.