[英]ng-pattern with regex having double quotes does not escape correctly
[英]Bash: Regex for finding pattern having double quotes
我試圖在我的shell腳本中使用正則表達式來查找子字符串。
原始字串:
"relative-to="jboss.server.base.dir" scan-enabled="true" scan-interval="0""
嘗試找到以下子字符串:
"scan-enabled="true""
碼:
str="relative-to=\"jboss.server.base.dir\" scan-enabled=\"true\" scan-interval=\"0\""
reg='scan-enabled.*"'
[[ "$str" =~ $reg ]] && echo $BASH_REMATCH
但是它回來了
scan-enabled="true" scan-interval="0"
有人可以幫忙如何使用正則表達式搜索涉及雙引號的模式嗎?
Bash版本:4.1.2(1)-發布
如果要匹配整個表達式scan-enabled="true"
或scan-enabled="false"
則可以嘗試以下操作:
reg='(scan-enabled=\"[^"]*\")'
[[ "$str" =~ $reg ]] && echo ${BASH_REMATCH[1]}
變量${BASH_REMATCH[1]}
將匹配正則表達式中的第一個捕獲組匹配項。 在這種情況下,整個正則表達式都包含在括號中,因此這是第一個捕獲組。
您可以通過以下鏈接探索此正則表達式:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.