爲什麼遵循正則表達式不匹配?爲什麼這個Bash正則表達式不起作用?
dev="$1"
regex="[a-zA-z]+([0-9]+)"
echo $dev
if [[ "$dev" =~ $regex ]]
then
echo "Got a match: ${BASH_REMATCH[1]}"
else
echo "Could not craete routing table reference due to incorrect device name [$dev]"
fi
這給了我一些在線測試工具的輸出
tun0
Could not craete routing table reference due to incorrect device name [tun0]
相同的正則表達式正常工作如。 http://regexraptor.net/
擊版本GNU bash, version 4.3.30(1)-release (x86_64-pc-linux-gnu)
您應該知道'[[...]'是一個*括號表達式*大小寫)範圍*校對元素*是區域設置相關的。要禁用語言環境依賴關係,您可以在頂部添加一個「export LC_ALL = C」行,並且您的正則表達式將按照您定義的方式「工作」。然而,在這種情況下,'A-z'會比你需要的匹配更多(''''''''',',''''等等)。 –