[英]Bash shell(grep) equivalent of this python regular expression?
[英]Regular expression on bash/shell/python for githook pre-commit
我正在嘗試使用正則表達式,但格式為字符串
[+/-] Added Feature 305105:WWE-108. Added Dolph Ziggler super star
讓我們看一下字符串的每個部分
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
我嘗試做的事情讓我們嘗試解決每個部分:
1) echo '[+]' | egrep -o "[+/-]+"
echo '[+]' | egrep -o "[+/-]+"
。 是的,它可以工作,但是,它也可以用於[+/]
或[/]
。 我看到沒有括號引號的結果
2) echo "Resolved" | egrep -o "Added$|Resolved$|Closed$"
echo "Resolved" | egrep -o "Added$|Resolved$|Closed$"
。 有用
3) echo '124214215215' | egrep -o "[0-9]+$"
echo '124214215215' | egrep -o "[0-9]+$"
。 有用
4) echo "Feature" | egrep -o "Feature$|Bug$|Fix$"
echo "Feature" | egrep -o "Feature$|Bug$|Fix$"
。 也可以
5)我還沒找到
6) echo "WWE-108" | egrep -o "[a-zA-Z]+-[0-9]+"
echo "WWE-108" | egrep -o "[a-zA-Z]+-[0-9]+"
。 也可以
7)我還沒找到8)任何文字
主要問題。 根據此模板,如何通過bash將所有這些點連接在一起。 [+/-] Added Feature 305105:WWE-108. Added Dolph Ziggler super star
[+/-] Added Feature 305105:WWE-108. Added Dolph Ziggler super star
。 我對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
但這不行。 您能否在bash上創建regexp來幫助我一點。 另外,python對我來說也沒關系。
為什么我要這樣做? 我想以這種格式制作預提交掛鈎。 [+/-] Added Feature 305105:WWE-108. Added Dolph Ziggler super star
[+/-] Added Feature 305105:WWE-108. Added Dolph Ziggler super star
。 這是一個道理,為什么我要這樣做。
來自評論的答案。 放在一起。
egrep '^\[(\+|-|\+/-)\] (Added|Resolved|Closed) (Feature|Bug|Fix) [0-9]+:[a-zA-Z]+-[0-9]+\..+'
一般規則,帶有擴展的正則表達式,元字符.*+^$(|)[]{}\\
必須以反斜杠轉義才能具有字面含義( []
之間的字符集中的規則不同)。
注意,對於文化而言,與基本正則表達式相反,反斜杠用於啟用正則表達式擴展名(|){}+
的特殊含義。
grep '^\[\(+\|-\|+/-\)\] \(Added\|Resolved\|Closed\) \(Feature\|Bug\|Fix\) [0-9]\+:[a-zA-Z]\+-[0-9]\+\..\+'
但是,它變得更長且更難理解。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.