Using Match Patterns in Conditions

Sometimes you may need a rule to check for a condition that matches a  pattern  as opposed to one that simply matches a fixed attribute (for example, “match any file with a three-digit number in its name,” as opposed to “match any file with the number 372 in its name”). Hazel offers an extensive pattern-building interface for just such situations. (You can also use patterns in actions, but the process is a bit different; see  Using Patterns in Actions .)

Hazel provides 11 built-in variables that you can use in constructing your match pattern, but if those are insufficient for your needs, you can create your own custom text, date, list, and table attributes to use in match patterns (see  Use Custom Attributes ). Custom attributes, in turn, are available to be used in later conditions and in the rule’s actions, enabling you to do things like rename files with only part of the original filename, or sort files into any of several subfolders based on their extension. (Again, refer to  Using Patterns in Actions  for details.)

For the following attributes, you can create patterns by choosing “matches” or “does not match” (or “match”/“do not match,” or “contain match”/“do not contain match,” depending on the context) from the second pop-up menu of the condition:

  • Name

  • Extension

  • Full Name

  • Tags

  • Comment

  • Contents

  • Source URL/Address

  • Other

For example, you might create a pattern that means “Name matches any word, followed by a hyphen, followed by any number.”

When you click in the field to edit a “match” or “does not match” pattern, a popover like this one appears:

Fill in the field at the top of this popover with the pattern you want. You can use a combination of plain text you type yourself (to include static elements as part of your pattern) and  tokens —blue bubbles representing any of numerous variables. To add a token to your pattern from the list below, either click it (once) or drag it to the desired position in the pattern.

For instance, in the image above, the pattern specifies a word (any sequence of letters) followed by a hyphen, followed by a number (one or more digits). Using that pattern, the name  blah-123  matches, but  123-blah  does not (since it doesn’t start with letters), nor does  blah123  (no hyphen after the word) or  blah-blah  (no number at the end).

Here’s what each token matches:

  • “Letter (a),” “Digit (1),” “Letter or Digit (a1),” “Symbol (%)” or “Character (a1%)”:  These tokens each match a  single character of the type(s) specified. The “Symbol” token matches any punctuation character while “Character” matches any non-blank character.

  • “Word (abc),” “Number (123),” “Letters & Digits (ab12),” “Symbols (%?@)” or “Characters (ab12%?)”:  These tokens each match a  sequence of characters of the type(s) specified. That is, “Word” matches any sequence of letters, Number matches any sequence of digits, and so on.

  • “Anything (…)”:  This token matches  any single character or group of characters or spaces, including nothing.  (It’s thus much like the  *  wildcard in most command-line environments.) If you have a pattern  (123)(…) , then the text  456  matches. The  “Number” (123)  token matches the  456 , and the  “Anything” (…)  token matches nothing.

The four custom attributes are even more powerful, in that they let you create named patterns of your own that are then available to other conditions and actions in this rule:

  • “Custom Text (●)”:  This token lets you create your own custom text-based attribute. See  Custom Text Attributes .

  • “Custom Date ( )”:  This token lets you create your own custom date attribute (that is, a particular date format). See  Custom Date Attributes .

  • “Custom List Item (—)”:  This token lets you create a list, such that your condition can match any of the items on the list. See  Custom List Attributes .

  • “Custom Table ( )”:  This token lets you create a table, such that your condition can match any of the items in a particular column, after which you can apply actions based on the contents of  another  column in the same row. See  Custom Table Attributes .

When you’re finished building your pattern, click Done to close the popover and continue creating or editing your rule.


Match Pattern Rules and Tips

Although most patterns are self-explanatory, you should be aware of a few special considerations:

  • Patterns are not case-sensitive (even with text you type manually).  A  is the same as a.

  • When a condition uses the “match”/“matches” or “does not match”/“do not match” instruction in the second pop-up menu, the pattern must match the  full text  of the attribute. If you want to be sure your condition also matches cases with additional text before and/or after your pattern, use the “Anything” (…) pattern token before and/or after your pattern. Using the example above, the text  45blah-123  does not match, because even though the last part after the  45  does match, the pattern has to start from the beginning and encompass everything until the end. Likewise,  blah-123ab  does not match because of the  ab  at the end. To match all these variants, the pattern could be changed to  (…)(abc)-(123)(…) .

  • In contrast to the preceding rule, when a condition uses the “contain match” instruction in the second pop-up menu (available with the “Contents” attribute and some of the “Other” attributes), the pattern only needs to match  part  of the text. (So, if the condition is “Contents contain match  (abc)-(123) ,” that would match a file that contains the text  A#3foo-75zxy , because the string  foo-75 , found within that text, matches the pattern. Also, a space in a “contain match” pattern matches any number of spaces (or other invisible characters).

  • With certain attributes (“Contents” and some of the “Other” attributes) an additional option appears at the bottom of the popover: “Match the ___ occurrence from the beginning/end.” This enables you to specify which occurrence you want to match, if there could be several (for example, “Match the 3rd occurrence from the beginning,” or “Match the 2nd occurrence from the end”). One reason to do this is to match only files that have a certain number of occurrences of a certain pattern—if you say “Match the 3rd occurrence from the beginning” but the file has only two occurrences, it won’t match. Another reason to do this is to create a custom text token (see  Custom Text Attributes ) that matches a particular occurrence of a string so that you can refer to it in a later condition or action in your rule. (For example, match the 3rd instance of a custom token representing a phone number, and display that phone number in a notification when the rule runs.)

  • When creating match patterns, it can be especially helpful to use Hazel’s live preview feature (see  Preview a Rule ) to test your pattern as you go.

SEE ALSO

Editing Date Patterns

Using Custom Attributes

Using Patterns in Actions