Bash 正则表达式匹配点和字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15226720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bash regex to match dots and characters
提问by Nick
I'm trying to use the =~operator to execute a regular expression pattern against a curl response string.
我正在尝试使用=~运算符对 curl 响应字符串执行正则表达式模式。
The pattern im currently using is:
我目前使用的模式是:
name\":\"(\.[a-zA-Z]+)\"
Currently however this pattern only extracts values that that contain only the characters a-z and A-Z. I need this pattern to also pick up values that contain a '.' character and a '@' character. How would I do this?
然而,目前此模式仅提取仅包含字符 az 和 AZ 的值。我需要这个模式来获取包含 '.' 的值。字符和“@”字符。我该怎么做?
Also, is there any way this pattern can be improved performance wise? It takes quite a long time to execute against the string.
另外,有什么办法可以提高这种模式的性能吗?对字符串执行需要很长时间。
Cheers.
干杯。
回答by dogbane
Working example script:
工作示例脚本:
#!/bin/bash
regex='"name":"([a-zA-Z.@]+)"'
input='"name":"internal.action.retry.queue@temp"'
if [[ $input =~ $regex ]]
then
echo "$input matches regex $regex"
for (( i=0; i<${#BASH_REMATCH[@]}; i++))
do
echo -e "\tGroup[$i]: ${BASH_REMATCH[$i]}"
done
else
echo "$input does not match regex $regex"
fi
回答by 3cheesewheel
I recently ran into this problem in my script that sets my bash prompt according to my git status, and found that it was because of the placement of other things (namely, a hyphen) I wanted to match inside the expression.
我最近在根据我的 git 状态设置我的 bash 提示的脚本中遇到了这个问题,发现这是因为我想在表达式中匹配其他东西(即连字符)的位置。
For example, I wanted to match a certain part of a git statusoutput, e.g. the part where it says "Your branch is ahead of 'origin/mybranch' by 1 commit."
例如,我想匹配git status输出的某个部分,例如它所说的部分"Your branch is ahead of 'origin/mybranch' by 1 commit."
This was my original pattern:
这是我原来的模式:
"Your branch is (ahead of|behind) '([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)' by ([0-9]+) commit".
One day I created a branch that had a .in it and found that my bash prompt wasn't showing me the right thing, and modified the expression to the following:
有一天,我创建了一个包含 a 的分支,.发现我的 bash 提示没有向我显示正确的内容,并将表达式修改为以下内容:
"Your branch is (ahead of|behind) '([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-.]+)' by ([0-9]+) commit".
I expected it to work just fine, but instead there was no match at all.
我希望它可以正常工作,但根本没有匹配项。
After reading a lot of posts, I realized it was because of the placement of the hyphen (-); I had to put it right after the first square bracket, otherwise it would be interpreted as a range (in this case, it was trying to interpret the range of _-., which is invalid or just somehow makes the whole expression fall over.
看了很多帖子,才明白是因为连字符(-)的位置;我必须把它放在第一个方括号之后,否则它会被解释为一个范围(在这种情况下,它试图解释 的范围_-.,这是无效的或者只是以某种方式使整个表达式失败。
It started working when I changed the expression to the following:
当我将表达式更改为以下内容时,它开始工作:
"Your branch is (ahead of|behind) '([a-zA-Z0-9_-]+)/([-a-zA-Z0-9_.]+)' by ([0-9]+) commit".
So basically what I meant to say that it could be something else in your expression (like the hyphen in mine) that is interfering with the matching of the dot and ampersand.
所以基本上我的意思是说它可能是你表达中的其他东西(比如我的连字符)干扰了点和&符号的匹配。
回答by Mikhail Vladimirov
Just add dot ('.') and at sign ('@'):
只需添加点 ('.') 和符号 ('@'):
name\":\"(\.[a-zA-Z.@]+)\"
If you don't need mandatory dot at the beginnig of the URL, use this:
如果在 URL 的开头不需要强制点,请使用以下命令:
\"name\":\"([a-zA-Z.@]+)\"

