gpt4 book ai didi

正则表达式匹配行尾 $ 在 Bash 脚本中不起作用

转载 作者:行者123 更新时间:2023-11-29 09:29:54 26 4
gpt4 key购买 nike

我正在尝试在 bash 脚本中执行一个简单的正则表达式语句,该语句将匹配并替换单词的结尾。以下是我正在尝试做的事情。

wordh > word:’

下面是我正在使用的代码。

#!/bin/bash
STAT=${STAT/h$/:’}

我不熟悉 bash 脚本,我认为它与 $ 有关,因为它用于标记变量。我试图转义它并在它之后添加另一个 / 。当我删除 $ 时它起作用了(不检查单词的结尾)。

最佳答案

那里的正则表达式有点不同。尝试:

STAT=${STAT/%h/:’}

来自手册页:

${parameter/pattern/string}

.         The pattern is expanded to produce a pattern just as in pathname
expansion. Parameter is expanded and the longest match of pat-
tern against its value is replaced with string. If Ipattern
begins with /, all matches of pattern are replaced with string.
Normally only the first match is replaced. If pattern begins
with #, it must match at the beginning of the expanded value of
parameter. If pattern begins with %, it must match at the end
of the expanded value of parameter. If string is null, matches
of pattern are deleted and the / following pattern may be omit-
ted. If parameter is @ or *, the substitution operation is
applied to each positional parameter in turn, and the expansion
is the resultant list. If parameter is an array variable sub-
scripted with @ or *, the substitution operation is applied to
each member of the array in turn, and the expansion is the
resultant list.

关于正则表达式匹配行尾 $ 在 Bash 脚本中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34690430/

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