# Pattern Matching Syntax

Wildcard patterns can be used in many functions in Opus, including:

* When searching for a file using the [Find Files](../../basic_concepts/searching_and_filtering/find_files) tool (matching both filename or file contents or metadata)
* When [filtering](../../basic_concepts/searching_and_filtering) files in and out of the display
* When [selecting](../../basic_concepts/selecting_files/simple_wildcard_selection) files in the file display
* When [filtering file operations](../../file_operations/filtered_operations) like copy or move

As well as [regular expressions](regular_expression_syntax), Opus supports a more simple wildcard system, called the _standard pattern matching_ system. When using wildcards, you specify a pattern consisting of a mix of literal text and special _wildcard tokens_. Each token is used to match one or more characters in the target string. As a simple example, the pattern **\*.doc** matches anything that ends with the literal characters _.doc_ (or in other words, document files).

| Token     | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |
| --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| **#**     | <p>The character or expression following the <strong>#</strong> is repeated 0 or more times.</p><p>For example:<br><strong>a#xb</strong> matches <em>ab</em>, <em>axb</em>, <em>axxb</em>, <em>axxxb</em>, etc.<br><strong>a#(xyz)b</strong> matches <em>ab</em>, <em>axyzb</em>, <em>axyzxyzb</em>, etc.</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      |
| **?**     | <p>Matches any single character.</p><p>For example:<br><strong>a?b</strong> matches <em>aab</em>, <em>abb</em>, <em>acb</em>, <em>adb</em>, etc. It does <strong>not</strong> match <em>ab</em>.</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |
| **\|**    | <p>Used to separate multiple expressions, any of which may match (effectively an <strong>or</strong> operator).</p><p>For example:<br><strong>a(x|y|z)b</strong> matches <em>axb</em>, <em>ayb</em> and <em>azb</em>.<br><strong>*.(gif|bmp|jpg)</strong> matches anything ending with <em>.gif</em>, <em>.bmp</em> or <em>.jpg</em>.</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
| **\~**    | <p>This is the <strong>not</strong> operator, it negates the following expression.</p><p>For example:<br><strong>~(*.doc)</strong> matches anything that doesn't end in <em>.doc</em><br><strong>~(*.(gif|bmp))</strong> matches anything that doesn't end in <em>.gif</em> or <em>.bmp</em></p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
| **()**    | <p>Parentheses are used to combine multiple characters into an expression. If we take the above example of <strong>a#(xyz)b</strong>, the parentheses are used to form <em>xyz</em> into a single expression. Without the brackets (i.e. <strong>a#xyzb</strong>) only the <em>x</em> would be seen to follow the <strong>#</strong> character - <em>yzb</em> would be treated literally.<br>Parentheses can be nested (as in the above example for <strong>~</strong>) to combine multiple expressions into a larger expression.</p><p>For example:<br><strong>NEW-((*.doc)|(*_backup_*))</strong> matches anything starting with <em>NEW-</em> that either ends in <em>.doc</em>, or is followed by the string <em>_backup_</em>.</p>                                                                                                                                                                                                                                                                            |
| **\[]**   | <p>Matches any single character in the set of specified characters.<br>You can specify the character set as individual characters (e.g. <strong>[abdfg]</strong>) or as a range of characters (e.g. <strong>[a-j]</strong>) or as multiple ranges.</p><p>For example:<br><strong>[abc]</strong> matches either <em>a</em>, <em>b</em> or <em>c</em><br><strong>[af-j]</strong> matches either <em>a</em>, <em>f</em>, <em>g</em>, <em>h</em>, <em>i</em> or <em>j</em><br><strong>[a-dh-kq-]</strong> matches <em>a</em>, <em>b</em>, <em>c</em>, <em>d</em>, <em>h</em>, <em>i</em>, <em>j</em>, <em>k</em>, or any character from <em>q</em> onwards<br><strong>IMGP#[0-9].jpg</strong> matches <em>IMGP0158.jpg</em> (or any other number).</p>                                                                                                                                                                                                                                                                 |
| **\[\~]** | <p>Matches any character <strong>not</strong> in the set of specified characters. See <strong>[]</strong> for information on how the set is defined.</p><p>For example:<br><strong>[~pqr]</strong> matches any character except <em>p</em>, <em>q</em> or <em>r</em></p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| **\***    | <p>Matches any number of characters, including zero. This is a synonym for <strong>#?</strong>.<br><strong>*.doc</strong> is equivalent to <strong>#?.doc</strong>.<br>For example:<br><strong>abc*xyz.*q</strong> matches any filename that begins with <em>abc</em>, has <em>xyz</em> at the end and then has any file extension ending with <em>q</em>.</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |
| **'**     | <p>The apostrophe is called the <em>escape character</em>.</p><p>Any character in the pattern string that isn't a wildcard token is taken as a literal character - literal characters have to match the characters in the target string exactly. However, if actually want to use a wildcard token as a literal character itself, you must escape the token by preceding it with an apostrophe.</p><p>For example:<br><strong>a#xb</strong> matches <em>ab</em>, <em>axb</em>, <em>axxb</em>, <em>axxxb</em>, etc, as seen above<br><strong>a'#xb</strong> will only match <em>a#xb</em></p>                                                                                                                                                                                                                                                                                                                                                                                                                       |
| **grp:**  | <p>When using wildcards to match filenames this is a shorthand way to refer to all the file extensions defined by a <a href="../../file_types/file_type_groups">file type group</a>.<br>For example, the <strong>Images</strong> group may contain the file extensions <em>.jpg</em>, <em>.bmp</em> and <em>.gif</em>. If you wanted a command to automatically select files of these types, you could use the command:<br><strong>Select *.(jpg|bmp|gif)</strong><br>A better way would be to use the <strong>grp:</strong> token to automatically include all file extensions from that group:<br><strong>Select grp:Images</strong></p><p>This is much easier to read (and type!), and if you ever add or remove file types from the group in question, any patterns which refer to the group will automatically reflect the new changes.</p><p>For example:<br><strong>(grp:Images|grp:Documents)</strong> matches any files in the <strong>Images</strong> or <strong>Documents</strong> file type groups</p> |
