Bash脚本模式匹配 [英] Bash script pattern matching

查看:122
本文介绍了Bash脚本模式匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要找到6位数字的模式,而前3位数字是特定数字,但是其余3位数字将是任意数字. 例如,以123开头的6位数字字符串,后跟任意3位数字.

I need a to find patterns that are 6 digits and the first 3 digits are specific digits, but the remaining 3 digits will be any digit. For example, 6 digit strings starting with 123 followed by any 3 digits.

var1="abc,123111,"
var2="abcdefg,123222,"
var3="xyzabc,987111,"

if [[ $var1 == *",123ddd,"* ]] ; then echo "Pattern matched"; fi

其中ddd是任何数字. var1和var2将匹配模式,但var 3将不匹配.我似乎无法完全正确.

Where ddd are any digits. var1 and var2 would match the pattern but var 3 would not. I can't seem to get it just right.

推荐答案

使用字符类:[0-9]匹配09以及字符集中它们之间的每个字符-至少在Unicode中和子集字符集(US-ASCII,Latin-1,UTF-8)是从18的数字.因此,它与10个拉丁数字中的任何一个都匹配.

Use a character class: [0-9] matches 0, 9, and every character between them in the character set, which - at least in Unicode and subset character sets (US-ASCII, Latin-1, UTF-8) - are the digits 1 through 8. So it matches any one of the 10 Latin digits.

if [[ $var1 == *,123[0-9][0-9][0-9],* ]] ; then echo "Pattern matched"; fi

使用=~代替==,将模式类型从外壳标准"glob"模式更改为正则表达式(简称"regexes").您可以将等效的正则表达式缩短一些:

Using =~ instead of == changes the pattern type from shell standard "glob" patterns to regular expressions ("regexes" for short). You can make an equivalent regex a little shorter:

if [[ $var1 =~ ,123[0-9]{3}, ]] ; then echo "Pattern matched"; fi

第一个缩写来自这样一个事实,即正则表达式只必须匹配字符串的任何部分,而不是整个字符串.因此,您不需要与在glob模式中找到的前导*和后导*等效.

The first shortening comes from the fact that a regex only has to match any part of the string, not the whole thing. Therefore you don't need the equivalent of the leading and trailing *s that you find in the glob pattern.

第二个长度减少是由于{n}语法引起的,该语法使您可以指定上一个模式的精确重复次数,而不必在正则表达式中实际重复模式本身. (您也可以通过指定最小值和最大值来匹配任意范围的重复计数,例如[0-9]{2,4}来匹配连续的两位,三位或四位数字.)

The second length reduction is due to the {n} syntax, which lets you specify an exact number of repetitions of the previous pattern instead of actually repeating the pattern itself in the regex. (You can also match any of a range of repetition counts by specifying a minimum and maximum, such as [0-9]{2,4} to match either two, three, or four digits in a row.)

值得注意的是,您还可以使用命名字符类来匹配数字.根据您的语言环境,[[:digit:]]可能与[0-9]完全相同,或者它可能包含来自其他脚本的具有Unicode数字,十进制数字"属性的字符.

It's worth noting that you could also use a named character class to match digits. Depending on your locale, [[:digit:]] may be exactly equivalent to [0-9], or it may include characters from other scripts with the Unicode "Number, Decimal Digit" property.

if [[ $var1 =~ ,123[[:digit:]]{3}, ]] ; then echo "Pattern matched"; fi

这篇关于Bash脚本模式匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆