takeaway
This is the third article in the string handling series. The first two articles basically covered common operations in string processing, but did not expand in detail on string matching.
Wildcard characters (globs) are an important concept in the shell and can be considered a simplified version of regular expressions. Wildcards are useful for things like string matching and file name searching. This article will focus only on string matching.
The basic usage of wildcards
The wildcard is the asterisk (*$STR “*).
% str1=abcd
% str2=bc
The # asterisk should be outside the quotation marks[[%"$str1"= = *"$str2"*]] &&echo good
good
# Note that wildcard strings must be placed on the right[[% *"$str2"* = ="$str1"&&]]echo goodCopy the code
The asterisk is the most common wildcard, and is used to match any number of characters, including zero.
The # question mark matches an arbitrary character
% [[ abcd == ab?? ]] && echo good
good
# Brackets are used to match individual characters that appear within them
% [[ abcd == abc[bcd] ]] && echo good
good
# If the first character in parentheses is ^, the single character in parentheses is matched
% [[ abcd == abc[^de] ]] && echo good
% [[ abcd == abc[^ce] ]] && echo good
good
The range of characters can be specified in parentheses
% [[ a4 == [a-b][2-5] ]] && echo good
good
# Multiple character ranges can be specified, and other characters can be doped
% [[ B4 == [a-cdddA-B][2-5] ]] && echo good
good
Angle brackets are used to match a range of single integers% [[123 == 12<3-4>]] &&echo good
good
# matches the whole integer% [[123 == <100-200>]] &&echo good
good
The default lower bound is 0, and the upper bound is infinity% [[123 == <100-> && 123 == <-200>]] &&echo good
good
# can have no upper and lower bounds, so it will match any positive integer and 0
This can be used to determine whether a string constitutes an integer
# [[ 123 == <-> ]] && echo good
good
# (1 | 2 |...). Used to judge multiple conditions at the same time, one can be satisfied
% [[ ab == (aa|ab) ]] && echo good
good
# If you want to use - or ^ in parentheses, put it at the end
% [[ -^3 == [a-c-][3^-][3^-] ]] && echo good
goodCopy the code
That’s the basic usage of wildcards, to sum up.
The wildcard | meaning | |
---|---|---|
* | Any number of any characters | |
? | Any character | |
[abcd] | Any character in abcd | |
[^abcd] | Any character except abcd | |
[a-c] | A character between a and C | |
[a-cB-Dxyz] | A character between a and C, B and D, and xyz | |
1-100 > < | An integer between 1 and 100 | |
< – > 50 | An integer between 0 and 50 | |
100 – > < | An integer greater than 100 | |
<-> | Any positive integer and 0 | |
([a-c]I<1-100>) | A character between a and C or an integer between 1 and 100 |
Enhanced wildcard
Zsh also supports enhanced wildcards, which have a little more functionality. If you use the enhanced wildcard, add setopt EXTENDED_GLOB to your code first.
The wildcard | meaning | A matching example |
---|---|---|
^abc | Any string except ABC | aaa |
abc^abc | A string that starts with ABC but is not followed by ABC | abcabd |
a*c~abc | A string conforming to a* C but not ABC | adc |
a# | Any number of a’s including 0 | aaa |
b## | One or more B’s | b |
(ab)## | One or more ab’s | abab |
(#i)abc | Ignore case ABC | AbC |
(#i)ab(#I)c | Ignore case ab followed by c | ABc |
(#l)aBc | A and C ignore case, but B must be capitalized aBc | aBC |
(#a1)abc | ABC with at most one character of error | A2c or AB or ABcd |
There are some more advanced uses that I’ll skip for now.
conclusion
We’ll leave strings behind, but we’ll keep touching on strings in future articles, because arrays and hashes are usually strings, there’s a lot of string manipulation involved with directory files, and so on, and there’ll be new ways to handle strings. In addition, I’ll update these articles if I find new ways or tricks to work with strings.
reference
www.bash2zsh.com/zsh_refcard…
Full article address: github.com/goreliu/zsh…
Pay to solve Windows, Linux, Shell, C, C++, AHK, Python, JavaScript, Lua and other fields related problems, flexible pricing, welcome to consult, wechat LY50247.