Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex Bash脚本正则表达式问题_Regex_Bash - Fatal编程技术网

Regex Bash脚本正则表达式问题

Regex Bash脚本正则表达式问题,regex,bash,Regex,Bash,我已经得到了下面的代码,并查看了许多示例,但无法找出我做错了什么。我知道正则表达式可以工作(请参阅)——只是在我的bash脚本中没有。这是一个git更新钩子,但显然我做错了什么!以下是我得到的: regex='ref:( |)([D]|[U])([E]|[S])(\d+)'; string="My commit ref: US2233556" if [[ $string =~ $regex ]]; then echo "[SUCCESS] Your message contains

我已经得到了下面的代码,并查看了许多示例,但无法找出我做错了什么。我知道正则表达式可以工作(请参阅)——只是在我的bash脚本中没有。这是一个git更新钩子,但显然我做错了什么!以下是我得到的:

regex='ref:( |)([D]|[U])([E]|[S])(\d+)';
string="My commit ref: US2233556"

if [[ $string =~ $regex ]];
  then
    echo "[SUCCESS] Your message contains ref: for a Story or Defect."
    exit 0
else
    echo "[POLICY] Your message is not formatted correctly. Please include a \"ref: USXXXXX\" or \"ref: DEXXX\" in the commit message."
    exit 1
fi

我将感谢任何帮助!谢谢大家!

您应该使用
[0-9]
而不是
\d
,并且您可以将交替的字符类合并为单个类(
[d]|[U]
=
[DU]
):

如果您没有使用捕获组,只需删除它们:

regex='ref: ?[DU][ES][0-9]+';

这是。请注意,
(|)
可以写得更短,如
(?)
(?)
,这样可以减少回溯。

使用
regex='ref:(|)([DU])([ES])([0-9]+)。说真的,那是我的主要问题?!是的,这是主要问题。Bash不支持PCRE正则表达式的味道,它要差得多。哦,天哪!非常感谢。我会尽快接受答案的!
regex='ref: ?[DU][ES][0-9]+';