1. Help Center
  2. Build
  3. Regular Expressions (Regex)

About Regular Expressions

A Regular Expression (frequently referred to as regex) is a tool used by many applications to search content for a pattern of text. Regex is not unique to SiteSpect; it is a pattern matching method used by many applications. SiteSpect makes use of regex to perform its search operations.

Regular Expression Characters

In regex, there are basically two types of characters:

  • Regular characters, or literal characters, which means that the character is what it looks like. The letter "a" is simply the letter "a". A comma "," is simply a comma and has no special meaning.

  • Special characters, or metacharacters, which means that the character has a special meaning. The character "?" is not a question mark; it has a special meaning. Likewise, the character "\" is not a backslash; it, too, has a special meaning. Regex makes use of the following special characters to interpret patterns of text:

    . ? * + ^ | $ \ ( ) [ ] { }

    If you want use one of these characters as a literal character, you must escape it by placing a backslash before it as follows:

    \. \? \* \+ \^ \| \$ \\ \( \) \[ \] \{ \}

How Do Regular Expressions Operate?

The basic regular expressions operate the same way as the Find function in standard editors like Microsoft Word or Notepad. Just like with the Find function, you ask the application to locate a string (or pattern) of text that you want to find. Sometimes, you may want to replace that text with a different string of text, but sometimes, you may want to carry out some other text manipulation task such as changing the order of your content.

For example, you can use regex to search for the text "Sign in" using the following search text: Sign in. It looks very similar to using the Find function in a text editor. You simply search for the literal text you want to find.

The advanced regex features allow you to search with more flexibility. With regex you can:

  • Search for a string from a list of possible text strings.
  • Find multiple text strings in a single search.
  • Use wildcards or ignore the text between terms you are searching for.
  • Copy or rearrange the text found by the search.

For example, to find either "Sign in" or "Sign out" you can use the regex: Sign (in|out). In regex, the pipe (|) character is a special character that means find either the part of the pattern on the left or the right side of the pipe.

The three most useful regex operations are:

  • Alternation – Allows you to specify a list of patterns to look for; any one of the patterns satisfies the search.

  • Wildcarding – Allows you to match any text instead of specific text, which makes it possible for you to match a character (or more than one character) that is variable or to avoid specifying the entire pattern you want to look for.

  • Grouping – Serves several purposes. Its two main uses are:

    • Constraining the alternation list to a subsection of the entire pattern.

    • Capturing (remembering) part of the matched text for later use in the replacement.