var [1] "a1" "a2" "a3" "a4" 这是我想要实现的目标:使用正则表达式并更改如下字符串: 3*a1 + a1*a2 + 4*a3*a4 + a1*-6ren">
gpt4 book ai didi

regex - R 正则表达式 : how to remove "*" only in between a group of variables

转载 作者:行者123 更新时间:2023-12-05 00:18:59 25 4
gpt4 key购买 nike

我有一组变量 var :

> var
[1] "a1" "a2" "a3" "a4"

这是我想要实现的目标:使用正则表达式并更改如下字符串:
 3*a1 + a1*a2 + 4*a3*a4 + a1*a3


 3a1 + a1*a2 + 4a3*a4 + a1*a3

基本上,我想修剪不在 var 中的任何值之间的“*” .先感谢您

最佳答案

可以做找(?<![\da-z])(\d+)\*替换 $1

 (?<! [\da-z] )
( \d+ ) # (1)
\*

或者, ((?:[^\da-z]|^)\d+)\*对于断言受损的引擎
 (                             # (1 start)
(?: [^\da-z] | ^ )
\d+
) # (1 end)
\*

无论如何,领先的断言都是不好的。

Benchmark
Regex1:   (?<![\da-z])(\d+)\*
Options: < none >
Completed iterations: 100 / 100 ( x 1000 )
Matches found per iteration: 2
Elapsed Time: 1.09 s, 1087.84 ms, 1087844 µs


Regex2: ((?:[^\da-z]|^)\d+)\*
Options: < none >
Completed iterations: 100 / 100 ( x 1000 )
Matches found per iteration: 2
Elapsed Time: 0.77 s, 767.04 ms, 767042 µs

关于regex - R 正则表达式 : how to remove "*" only in between a group of variables,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36896042/

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