简体   繁体   English

bash / shell / python上用于githook预提交的正则表达式

[英]Regular expression on bash/shell/python for githook pre-commit

I am trying to work with regular expression I have a string in format 我正在尝试使用正则表达式,但格式为字符串

[+/-] Added Feature 305105:WWE-108. Added Dolph Ziggler super star

Let's look on each part of string 让我们看一下字符串的每个部分

1) [+/-] – bracket quotes are important. it can [+] or [-]. or [+/-]. not "+", or "-", or "+/-" without bracket quotes
2) Added – it can be "Added", "Resolved", "Closed"
3) 305105 – any numbers
4) Feature – it can be "Feaute", "Bug", "Fix"
5) : – very imporant delimiter
6) WWE-108 – any text with delimiter "–" and with numbers after delimiter
7) . – very imporant delimiter
8) Added Dolph Ziggler super star – any text

What I tried to do Let's try to resolve each part: 我尝试做的事情让我们尝试解决每个部分:
1) echo '[+]' | egrep -o "[+/-]+" 1) echo '[+]' | egrep -o "[+/-]+" echo '[+]' | egrep -o "[+/-]+" . echo '[+]' | egrep -o "[+/-]+" Yes, it works, but, it works also for [+/] , or [/] . 是的,它可以工作,但是,它也可以用于[+/][/] and I see result without bracket quotes 我看到没有括号引号的结果
2) echo "Resolved" | egrep -o "Added$|Resolved$|Closed$" 2) echo "Resolved" | egrep -o "Added$|Resolved$|Closed$" echo "Resolved" | egrep -o "Added$|Resolved$|Closed$" . echo "Resolved" | egrep -o "Added$|Resolved$|Closed$" It works 有用
3) echo '124214215215' | egrep -o "[0-9]+$" 3) echo '124214215215' | egrep -o "[0-9]+$" echo '124214215215' | egrep -o "[0-9]+$" . echo '124214215215' | egrep -o "[0-9]+$" It works 有用
4) echo "Feature" | egrep -o "Feature$|Bug$|Fix$" 4) echo "Feature" | egrep -o "Feature$|Bug$|Fix$" echo "Feature" | egrep -o "Feature$|Bug$|Fix$" . echo "Feature" | egrep -o "Feature$|Bug$|Fix$" It works too 也可以
5) I have not found how 5)我还没找到
6) echo "WWE-108" | egrep -o "[a-zA-Z]+-[0-9]+" 6) echo "WWE-108" | egrep -o "[a-zA-Z]+-[0-9]+" echo "WWE-108" | egrep -o "[a-zA-Z]+-[0-9]+" . echo "WWE-108" | egrep -o "[a-zA-Z]+-[0-9]+" It works too 也可以
7) I have not found how 8) Any text 7)我还没找到8)任何文字

The main question. 主要问题。 How to concatenate, all these points via bash with spaces, according to this template. 根据此模板,如何通过bash将所有这些点连接在一起。 [+/-] Added Feature 305105:WWE-108. Added Dolph Ziggler super star [+/-] Added Feature 305105:WWE-108. Added Dolph Ziggler super star . [+/-] Added Feature 305105:WWE-108. Added Dolph Ziggler super star I am not familiar with regexp, as for me, I'd like to do something like this: 我对regexp不熟悉,对于我来说,我想做这样的事情:

string="[+/-] Added Feature 305105:WWE-108. Added Dolph Ziggler super star"
first=$(echo $string | awk '{print $1}')

if [[ $first == "[+]" ]]; then
echo "ok"
echo $first
elif [[ $first == "[*]" ]]; then
echo "ok2"
echo $first
elif [[ $first == "[+/-]" ]]; then
echo "ok3"
echo "$first"
else
echo "not ok"
echo $first
exit 1
fi

But it is not ok. 但这不行。 Can you please help me in a little bit with creation of regexp on bash. 您能否在bash上创建regexp来帮助我一点。 Also, python it is ok too for me. 另外,python对我来说也没关系。

Why I am doing this ? 为什么我要这样做? I want to make pre-commit hook, in format like this. 我想以这种格式制作预提交挂钩。 [+/-] Added Feature 305105:WWE-108. Added Dolph Ziggler super star [+/-] Added Feature 305105:WWE-108. Added Dolph Ziggler super star . [+/-] Added Feature 305105:WWE-108. Added Dolph Ziggler super star This is a reson, why I am doing this. 这是一个道理,为什么我要这样做。

Answer from comment. 来自评论的答案。 Putting all together. 放在一起。

egrep '^\[(\+|-|\+/-)\] (Added|Resolved|Closed) (Feature|Bug|Fix) [0-9]+:[a-zA-Z]+-[0-9]+\..+'

a general rule, with extended regex, meta characters .*+^$(|)[]{}\\ must be escaped with a backslash to have literal meaning (except in character sets between [] where rules are different). 一般规则,带有扩展的正则表达式,元字符.*+^$(|)[]{}\\必须以反斜杠转义才能具有字面含义( []之间的字符集中的规则不同)。

Note, for culture, that with basic regex, it's the contrary, backslash was used to enable the specaial meaning of regex extensions (|){}+ . 注意,对于文化而言,与基本正则表达式相反,反斜杠用于启用正则表达式扩展名(|){}+的特殊含义。

grep '^\[\(+\|-\|+/-\)\] \(Added\|Resolved\|Closed\) \(Feature\|Bug\|Fix\) [0-9]\+:[a-zA-Z]\+-[0-9]\+\..\+'

But it's longer and harder to understand. 但是,它变得更长且更难理解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM