Skip to end of banner
Go to start of banner

WIP: How to use regular expressions

Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

Overview

Macros such as Hide, Run without a user form, and Run with a user form and parameters make use of regular expressions for pattern matching. Generally, just a simple understanding of regular expressions and a few examples are enough to get by for most use cases. This page has a few simple examples to get started. Use the references for more advanced information. It is recommended to test your regular expressions in one of the well-known regex testing sites such as RegexPlanet or Regex101.

Key tips

  • Dot or period (.) is a special regex character. If you really want to match on it, you need to escape it with a backslash: \.
  • Don't be confused with generic pattern matching used for file systems for instance. On a file system, *.png means all files ending with .png. That is an illegal regex expression. For regex you need: .*\.png, or to simplify: .*png which finds all files ending in png (not necessarily ending in an extension of PNG).
  • Regex is case sensitive by default. In most cases, use the case insensitive flag: (?i). See one of the examples below.
  • Use the Dotall mode to match across line breaks. 

In dotall mode, the expression . matches any character, including a line terminator. By default this expression does not match line terminators. Dotall mode can also be enabled via the embedded flag expression (?s). (The s is a mnemonic for "single-line" mode, which is what this is called in Perl.)

Simple examples

Advanced examples

Value
Regex
Matches
FindDemonstrates
example.txt

^((?!\.png).)*$

(error)(tick)Find string not containing a word. In this example, files that do not have a .png extension
example.png

^((?!\.png).)*$

(error)(error)Find string not containing a word. In this example, files that do not have a .png extension
example.jpeg(?=^((?!\.png).)*$)(?=^((?!\.jpeg).)*$)(error)(error)Find string not containing a word. In this example, files that do not have a .png or .jpeg extension
collateral wholesale retail.*(?=.*\bretail\b.*)(?=.*\bcollateral\b.*).*(tick)
Match exact words anywhere in string. In this case, a blank separated list of labels and both collateral and retail must be included for the match to be successful
wholesale retail.*(?=.*\bretail\b.*)(?=.*\bcollateral\b.*).*(error)
Both are required for a match
merger acquisition.*\b(?:merger|acquisition)\b.*(tick)
Match string containing either word

References

  • No labels