Linux命令进行通配符匹配
是否有任何 bash 命令可以执行类似以下操作:
if [[ $string =~ $pattern ]]
但它适用于简单的通配符(?,*)而不是复杂的正则表达式?
更多信息:
我有一个配置文件(一种类似 .ini 的文件),其中每一行都由通配符模式和一些其他数据组成。
对于我的脚本收到的任何给定输入字符串,我必须找到配置文件中通配符模式与输入字符串匹配的第一行,然后返回该行中的其余数据。
这很简单。我只需要一种方法来将字符串与通配符模式匹配,而不是正则表达式,因为模式可能包含点、括号、破折号等,并且我不希望它们被解释为特殊字符。
Is there any bash command to do something similar to:
if [[ $string =~ $pattern ]]
but that it works with simple wild cards (?,*) and not complex regular expressions ??
More info:
I have a config file (a sort of .ini-like file) where each line is composed of a wild card pattern and some other data.
For any given input string that my script receives, I have to find the first line in the config file where the wild card pattern matches the input string and then return the rest of the data in that line.
It's simple. I just need a way to match a string against wild card patterns and not RegExps since the patterns may contain dots, brackets, dashes, etc. and I don't want those to be interpreted as special characters.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
[ -z ${string/$pattern} ]
技巧有一些非常严重的问题:如果字符串为空,它将匹配所有可能的模式;如果它包含空格,测试命令会将其解析为表达式的一部分(尝试使用string="x -o 1 -eq 1"
来娱乐)。 bash 的 [[ 表达式本身与 == 运算符进行 glob 样式通配符匹配,因此不需要所有这些复杂的(且容易出问题的)技巧。只需使用:The
[ -z ${string/$pattern} ]
trick has some pretty serious problems: if string is blank, it'll match all possible patterns; if it contains spaces, the test command will parse it as part of an expression (trystring="x -o 1 -eq 1"
for amusement). bash's [[ expressions do glob-style wildcard matching natively with the == operator, so there's no need for all these elaborate (and trouble-prone) tricks. Just use:有几种方法可以做到这一点。
在 bash >= 3 中,您有像您所描述的正则表达式匹配,例如请
注意,此语法使用正则表达式模式,因此它使用
.
而不是?
来匹配单个字符。如果您想做的只是测试字符串是否包含子字符串,那么还有更经典的方法,例如
您还可以通过这种方式测试字符串是否以表达式开头或结尾:
请参阅参数扩展man bash 的段落。分别使用
##
或%%
代替#
和%
将实现最长匹配而不是简单匹配。处理通配符的另一种非常经典的方法是用例:
There's several ways of doing this.
In bash >= 3, you have regex matching like you describe, e.g.
Note that this syntax uses regex patterns, so it uses
.
instead of?
to match a single character.If what you want to do is just test that the string contains a substring, there's more classic ways of doing that, e.g.
You can also test if a string begins or ends with an expression this way:
See the Parameter Expansion paragraph of man bash for more info on these syntaxes. Using
##
or%%
instead of#
and%
respectively will achieve a longest matching instead of a simple matching.Another very classic way of dealing with wildcards is to use case:
John T 的回答被删除了,但我实际上认为他是在正确的轨道上。这里是:
约翰没有考虑的是答案所要求的通配符。为此,请使用
egrep
,又名grep -E
,并使用正则表达式通配符.*
。这里,.
是通配符,*
是一个乘数,意思是“任意数量的这些”。因此,约翰的示例变为:.
通配符表示法是相当标准的正则表达式,因此它应该适用于任何使用正则表达式的命令。如果您需要转义特殊字符,它确实会变得令人讨厌,因此这可能不是最佳选择:
John T's answer was deleted, but I actually think he was on the right track. Here it is:
What John didn't consider is the wildcard requested by the answer. For that, use
egrep
, a.k.a.grep -E
, and use the regex wildcard.*
. Here,.
is the wildcard, and*
is a multiplier meaning "any number of these". So, John's example becomes:The
.
wildcard notation is fairly standard regex, so it should work with any command that speaks regex's.It does get nasty if you need to escape the special characters, so this may be sub-optimal: