有没有办法用正则表达式内联替换文本,而不是从变量中取出文本并将其存储在变量中?
我是初学者.我经常发现自己写作
my $foo = $bar;
$foo =~ s/regex/replacement/;
doStuff($foo)
Run Code Online (Sandbox Code Playgroud)
我真的很想写的地方
doStuff($bar->replace(s/regex/replacement/));
Run Code Online (Sandbox Code Playgroud)
或者类似,而不是使用临时变量和三行.
有没有办法做到这一点?显然,当正则表达式足够复杂时,将它拆分出来以便更好地解释是有意义的,但是当它只是s/\s//g用其他变量使代码混乱时感觉是错误的.
有一个文件dummy.txt
内容如下:
9/0/2010
9/2/2010
10/11/2010
Run Code Online (Sandbox Code Playgroud)
我必须将月份部分(0,2,11)改为+1,即(1,3,12)我写了替换正则表达式如下
$line =~ s/\/(\d+)\//\/\1+1\//;
Run Code Online (Sandbox Code Playgroud)
这是打印
9/0+1/2010
9/2+1/2010
10/11+1/2010
Run Code Online (Sandbox Code Playgroud)
如何使它添加 - 3数字而不是执行字符串concat?2+1??
如何用他们的定义替换所有出现的用户定义的乳胶宏?
例如,给定此文件
old.tex
\newcommand{\blah}[2]{#1 \to #2}
...
foo \blah{egg}{spam} bar
...
Run Code Online (Sandbox Code Playgroud)
如何以自动方式生成下面的文件
new.tex
...
foo egg \to spam bar
...
Run Code Online (Sandbox Code Playgroud)
而不是用perl重新实现乳胶宏逻辑,我可以使用latex或tex引擎来做到这一点吗?
每个人都说eval是邪恶的,你应该用$()代替.但是我遇到了一种情况,即$()内部的unquoting处理方式不同.
背景是我经常被包含空格的文件路径烧掉,所以喜欢引用所有这些路径.关于想知道我的所有可执行文件来自何处的更多偏执狂.更偏执,不相信自己,所以能够显示我即将运行的创建命令.
下面我尝试使用eval与$()的变体,以及是否引用命令名称(因为它可能包含空格)
BIN_LS="/bin/ls"
thefile="arf"
thecmd="\"${BIN_LS}\" -ld -- \"${thefile}\""
echo -e "\n Running command '${thecmd}'"
$($thecmd)
Running command '"/bin/ls" -ld -- "arf"'
./foo.sh: line 8: "/bin/ls": No such file or directory
echo -e "\n Eval'ing command '${thecmd}'"
eval $thecmd
Eval'ing command '"/bin/ls" -ld -- "arf"'
/bin/ls: cannot access arf: No such file or directory
thecmd="${BIN_LS} -ld -- \"${thefile}\""
echo -e "\n Running command '${thecmd}'"
$($thecmd)
Running command '/bin/ls -ld -- "arf"'
/bin/ls: cannot access "arf": No …Run Code Online (Sandbox Code Playgroud) 我尝试let使用以下语义实现一个函数:
> let(x = 1, y = 2, x + y)
[1] 3
Run Code Online (Sandbox Code Playgroud)
...在概念上有点类似于substitute语法with.
以下代码几乎可以工作(例如上面的调用工作):
let <- function (...) {
args <- match.call(expand.dots = FALSE)$`...`
expr <- args[[length(args)]]
eval(expr,
list2env(lapply(args[-length(args)], eval), parent = parent.frame()))
}
Run Code Online (Sandbox Code Playgroud)
注意嵌套eval,外部用于评估实际表达式,而inner用于评估参数.
不幸的是,后一种评估发生在错误的背景下.当尝试let使用检查当前帧的函数调用时,这变得很明显,例如match.call:
> (function () let(x = match.call(), x))()
Error in match.call() :
unable to find a closure from within which 'match.call' was called
Run Code Online (Sandbox Code Playgroud)
我想过提供父框架作为评估环境eval,但这不起作用:
let <- function (...) {
args …Run Code Online (Sandbox Code Playgroud) 我在Ubuntu 12.04上运行这个替换命令.
$ sed -e "s/([a-zA-Z0-9.-/\\ :]+)/\1/g"
Run Code Online (Sandbox Code Playgroud)
但是,会引发以下错误.
sed:-e expression#1,char 27:无效的范围结束
我记得同样的表达式适用于MacOSX.
你能描述命令失败的原因吗?
是否可以根据Lua中的列表替换字符,就像tr在Perl中一样?例如,我想替换A到B和B到A(如 AABBCC变BBAACC).
在Perl中,解决方案是$str ~= tr/AB/BA/.在Lua有这种本地方式吗?如果没有,我认为最好的解决方案是迭代整个字符串,因为单独的替换需要使用特殊符号来区分已经替换的字符和非字符的字符.
编辑:我的目标是计算反向互补的DNA串,描述在这里.
给定一组正则表达式,是否有一种简单的方法来匹配多个模式,并根据匹配的模式替换匹配的文本?
例如,对于以下数据x,每个元素以数字或字母开头,并以数字或字母结尾.让我们称这些模式num_num(以数字开头,以数字结尾),num_let(以数字开头,以字母结尾)let_num,和let_let.
x <- c('123abc', '78fdsaq', 'aq12111', '1p33', '123', 'pzv')
type <- list(
num_let='^\\d.*[[:alpha:]]$',
num_num='^\\d(.*\\d)?$',
let_num='^[[:alpha:]].*\\d$',
let_let='^[[:alpha:]](.*[[:alpha:]])$'
)
Run Code Online (Sandbox Code Playgroud)
要用它后面的模式名称替换每个字符串,我们可以这样做:
m <- lapply(type, grep, x)
rep(names(type), sapply(m, length))[order(unlist(m))]
## [1] "num_let" "num_let" "let_num" "num_num" "num_num" "let_let"
Run Code Online (Sandbox Code Playgroud)
有更有效的方法吗?
gsubfn?我知道gsubfn我们可以同时替换不同的比赛,例如:
library(gsubfn)
gsubfn('.*', list('1p33'='foo', '123abc'='bar'), x)
## [1] "bar" "78fdsaq" "aq12111" "foo" "123" "pzv"
Run Code Online (Sandbox Code Playgroud)
但我不确定替换是否可以依赖于匹配的模式而不是匹配本身.
stringr?str_replace_all不能很好地使用这个例子,因为迭代地将匹配替换为模式,我们最终会被覆盖的所有内容let_let:
library(stringr)
str_replace_all(x, setNames(names(type), unlist(type)))
## …Run Code Online (Sandbox Code Playgroud) 采用包含PHP样式变量的纯文本(不是PHP代码),然后替换变量值的最佳方法是什么.这有点难以描述,所以这是一个例子.
// -- myFile.txt --
Mary had a little $pet.
// -- parser.php --
$pet = "lamb";
// open myFile.txt and transform it such that...
$newContents = "Mary had a little lamb.";
Run Code Online (Sandbox Code Playgroud)
我一直在考虑使用正则表达式,或者也许eval(),虽然我不确定哪个最简单.这个脚本只会在本地运行,因此对安全问题的担忧eval()并不适用(我认为?).
我还要补充一点,我可以使用以下方法将所有必要的变量放入数组get_defined_vars():
$allVars = get_defined_vars();
echo $pet; // "lamb"
echo $allVars['pet']; // "lamb"
Run Code Online (Sandbox Code Playgroud)