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

use*_*743 6 regex bash eol parameter-expansion

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

wordh > word:’
Run Code Online (Sandbox Code Playgroud)

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

#!/bin/bash
STAT=${STAT/h$/:’}
Run Code Online (Sandbox Code Playgroud)

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

Joh*_*all 4

正则表达式有点不同。\n尝试:

\n\n
STAT=${STAT/%h/:\xe2\x80\x99}\n
Run Code Online (Sandbox Code Playgroud)\n\n

从手册页:

\n\n
\n

${参数/模式/字符串}

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

  • 更像是,它根本不是正则表达式。 (3认同)