- Article pages now have a discussion option at the bottom (moderated/captcha, but no registration needed)

Patterns and pattern matching

FIXME missing better extglob description; missing shell options to control matching, e.g. nocalseglob etc..

A pattern is a string description. Bash can use it in various situations:

The pattern description language is relatively easy. Any character that's not mentioned below matches itself. The NUL character may not occur in a pattern. If special characters are quoted, they're matched literally without their special meaning.

Normal pattern language

SequenceDescription
*Matches any string, including the null string (empty string)
?Matches any single character
XMatches the character X which can be any character that has no special meaning
\XMatches the character X, where the character's special meaning is taken away using the backslash
\\Matches a backslash
[…]Defines a pattern bracket expression (see below). Matches any of the enclosed characters at this position.

Bracket expressions

The bracket expression […] mentioned above has some useful specialities:

Bracket expressionDescription
[XYZ]The "normal" bracket expression, matching either X, Y or Z
[X-Z]A range expression: Matching all the characters from X to Y (whatever that means in your current locale, it depends how the characters are sorted!)
[[:class:]]Matches all the characters defined by a POSIX® character class: alnum, alpha, ascii, blank, cntrl, digit, graph, lower, print, punct, space, upper, word and xdigit
[^…]A negating expression: It matches all the characters that are not in the bracket expression
[!…]Equivalent to [^…]
[]...] or [-…]Used to include the characters ] and - into the set, they need to be the first characters after the opening bracket
[=C=]Matches any character that is eqivalent to the collation weight of C (current locale!)
[[.SYMBOL.]]Matches the collating symbol SYMBOL

Examples

Some simple examples using normal pattern matching:

  • Pattern "Hello world" matches
    • Hello world
  • Pattern [Hh]"ello world" matches
    • Hello world
    • hello world
  • Pattern Hello* matches (for example)
    • Hello world
    • Helloworld
    • HelloWoRlD
    • Hello
  • Pattern Hello world[[:punct:]] matches (for example)
    • Hello world!
    • Hello world.
    • Hello world+
    • Hello world?
  • Pattern [[.backslash.]]Hello[[.vertical-line.]]world[[.exclamation-mark.]] matches (using collation sybols)
    • \Hello|world!

Extended pattern language

If you set the shell option extglob, Bash understands some more powerful patterns. Here, a <PATTERN-LIST> is one or more pattern, separated by the pipe-symbol (|).

?(<PATTERN-LIST>)Matches zero or one occurrence of the given patterns
*(<PATTERN-LIST>)Matches zero or more occurrences of the given patterns
+(<PATTERN-LIST>)Matches one or more occurrences of the given patterns
@(<PATTERN-LIST>)Matches one of the given patterns
!(<PATTERN-LIST>)Matches anything except one of the given patterns

Examples

Delete all but one specific file

rm -f !(survivior.txt)

Pattern matching configuration

Related shell options

option classification description
dotglob globbing see Pathname expansion customization
extglob global enable/disable extended pattern matching language, as described above
failglob globbing see Pathname expansion customization
nocaseglob globbing see Pathname expansion customization
nocasematch pattern/string matching perform pattern matching without regarding the case of individual letters
nullglob globbing see Pathname expansion customization
globasciiranges globbing see Pathname expansion customization

Discussion

Amit Verma, 2010/09/16 14:30

What if we want to match a particular pattern for 'n' number of times??

Jan Schampera, 2010/09/17 06:47

This is not possible with patterns (or extended patterns). Patterns like this are not a substitute for regular expressions (but infact usually patterns can do the most "common" work, sometimes people (ab)use regular expressions for very very easy tasks in shell coding). Maybe Bash will have a new feature like this for extended patterns in future.

Depending on your needs you could either use some external program (grep, awk, sed, ...) or the conditional expression with the =~ operator. A pathname expansion with regular expressions is not possible.

Tim Jones, 2012/07/11 10:32

Why does bash need extglob enabled to enable those glob 'extensions' when they've been available in ksh for decades, by default?

This is causing many problems as scripts cannot be compatible with both ksh and bash :(

Jan Schampera, 2012/07/12 23:45

I think this is a question you should ask the maintainer. I don't think there's a way to make different shells 100% compatible, not even in theory.

Enter your comment
 
syntax/pattern.txt · Last modified: 2013/04/14 13:56 by thebonsai
GNU Free Documentation License 1.3
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0