简体   繁体   English

Shell脚本中正则表达式中的通配符

[英]wildcard in regular expression in shell script

I have a question regarding wildcard in shell script regular exression vi a.sh 我对shell脚本常规扩展名vi a.sh中的通配符有疑问

if [[ $1 == 1*3 ]]; then
  echo "matching"
else
  echo "not matching"
fi

If I run sh a.sh 123 the output is: "matching". 如果我运行sh a.sh 123 ,输出为:“ matching”。

But according to http://www.tldp.org/LDP/GNU-Linux-Tools-Summary/html/x11655.htm : 但是根据http://www.tldp.org/LDP/GNU-Linux-Tools-Summary/html/x11655.htm

* (asterisk) *(星号)

the proceeding item is to be matched zero or more times. 进行中的项目应匹配零次或多次。 ie. 即。 n* will match n, nn, nnnn, nnnnnnn but not na or any other character. n *将匹配n,nn,nnnn,nnnnnnn,但不匹配na或任何其他字符。

it should match to only 3,13,113,111..3. 它只能匹配3,13,113,111.3。

But why is it matching 123? 但是为什么它匹配123?

In the documentation you linked you are taking the part in which it talks about "Regular expressions". 您链接文档中,您参与其中涉及“正则表达式”的部分。

However, what is important here is what is within "Standard Wildcards (globbing patterns)": 但是,此处重要的是“标准通配符(globbing模式)”中的内容:

Standard Wildcards (globbing patterns) 标准通配符(通配模式)

this can represent any number of characters (including zero, in other words, zero or more characters). 它可以代表任意数量的字符 (包括零个字符 ,换句话说,零个或多个字符)。 If you specified a "cd*" it would use "cda", "cdrom", "cdrecord" and anything that starts with “cd” also including “cd” itself. 如果指定了“ cd *”,它将使用“ cda”,“ cdrom”,“ cdrecord”以及任何以“ cd”开头的内容,还包括“ cd”本身。 "m*l" could by mill, mull, ml, and anything that starts with an m and ends with an l. “ m * l”可以用磨,仔细考虑,ml以及任何以m开头并以l结尾的东西表示。

That is, it does not refer to the previous character but to a set of characters (zero or more). 也就是说,它不引用前一个字符,而是引用一组字符(零个或多个)。 It is what equivalent to a .* in normal regular expressions. 在正常的正则表达式中,它等同于.*

So the expression 1*3 matches anything starting with 1 + zero or more characters + 3 at the end. 因此表达式1*3匹配以1 + 0或更多字符+ 3结尾的任何内容。

There are two different pattern matching you find in the Unix/Linux World: 在Unix / Linux世界中可以找到两种不同的模式匹配:

  • Regular Expressions : This is the complex pattern matching you find in grep , sed , and many other utilities. 正则表达式 :这是您在grepsed和许多其他实用程序中找到的复杂模式匹配。 As time moved on, many extensions can be found. 随着时间的流逝,可以找到许多扩展。 These extensions are referred to in POSIX as original (now considered obsolete), modern , and extended . 这些扩展在POSIX中被称为original (现在认为已过时), modernextended
  • Globbing : This refers to the file matching you find when you do things such as *.txt to match all text files. Globbing :这是指您在执行*.txt以匹配所有文本文件时找到的文件匹配项。 These are much simpler and less extensive. 这些要简单得多,范围也不广。 There are a few extensions (like ** to match subdirectories in Ant). 有一些扩展名(例如**以匹配Ant中的子目录)。

When you use [[ ... == ... ]] without quotes in Bash, you are using globbing file matches. 当您使用[[ ... == ... ]]在Bash中不带引号时,您使用的是文件匹配。 If you want to use regular expressions, you need to use the =~ operator: 如果要使用正则表达式,则需要使用=~运算符:

if [[ $foo =~ ^11*3 ]]   # Matches 13, 113, 1113, 11113
then

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM