问题描述
我想再次并称字(以避免重复)之前,匹配列表中的一个单词的presence。我使用bash 4.2.24和我尝试如下:
I am trying to match on the presence of a word in a list before adding that word again (to avoid duplicates). I am using bash 4.2.24 and am trying the below:
[[ $foo =~ \bmyword\b ]]
也
[[ $foo =~ \<myword\> ]]
然而,无论是似乎工作。它们在bash的文档例子提到:http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_04_01.html.
我presume我做错了什么,但我不知道是什么。任何指导AP preciated ...
I presume I am doing something wrong but I am not sure what. Any guidance appreciated...
推荐答案
是的,所有列出的正则表达式扩展支持,但你有更好的运气投入变量模式使用它之前。试试这个:
Yes, all the listed regex extensions are supported but you'll have better luck putting the pattern in a variable before using it. Try this:
re=\\bmyword\\b
[[ $foo =~ $re ]]
周围挖我发现,其答案似乎可以解释为什么更改的行为时,正则表达式是内联写为你的榜样。你可能会不得不重写你的测试,以便使用一个临时变量为你的正则表达式,或者使用3.1兼容模式:
Digging around I found this question, whose answers seems to explain why the behaviour changes when the regex is written inline as in your example. You'll probably have to rewrite your tests so as to use a temporary variable for your regexes, or use the 3.1 compatibility mode:
shopt -s compat31
这篇关于不支持的bash字边界定期EX pressions?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!