SonicOS 8 Rules and Policies for Classic Mode

Creating a Regular Expression in a Match Object

Predefined regular expressions can be selected during configuration, or you can configure a custom regular expression. This use case describes how to create a Regex Match object for a credit card number, while illustrating some common errors.

For example, a user creates a Regex Match object for a credit card number, with the following inefficient and also slightly erroneous construction:

[1-9][0-9]{3} ?[0-9]{4} ?[0-9]{4} ?[0-9]{4}

Using this object, the user attempts to build a policy. After the user clicks OK, the appliance displays a “Please wait…” message, but the management session is unresponsive for a very long time and the regular expression might eventually be rejected.

This behavior occurs because, in custom object and file content match objects, regular expressions are implicitly prefixed with a dot asterisk (.*). A dot matches any of the 256 ASCII characters except ‘\n’. This fact, the match object type used, and the nature of the regular expression in combination causes the control plane to take a long time to compile the required data structures.

The fix for this is to prefix the regular expression with a '\D'. This means that the credit card number is preceded by a non-digit character, which actually makes the regular expression more accurate.

Additionally, the regular expression shown above does not accurately represent the intended credit card number. The regular expression in its current form can match several false positives, such as 1234 12341234 1234. A more accurate representation is the following:

\D[1-9][0-9]{3} [0-9]{4} [0-9]{4} [0-9]{4}

or

\D[1-9][0-9]{3}[0-9]{4}[0-9]{4}[0-9]{4}

which can be written more concisely as:

\D\z\d{3}( \d{4}){3}

or

\D\z\d{3}(\d{4}){3}

respectively.

These can be written as two regular expressions within one match object or can be further compressed into one regular expression such as:

\D\z\d{3}(( \d{4}){3}|(\d{12}))

You can also capture credit card numbers with digits separated by a '-' with the following regular expression:

\D\z\d{3}(( \d{4}){3}|(-\d{4}){3}|(\d{12}))

The preceding ‘\D’ should be included in all of these regular expressions.

Was This Article Helpful?

Help us to improve our support portal

Techdocs Article Helpful form

  • Hidden
  • Hidden

Techdocs Article NOT Helpful form

  • Still can't find what you're looking for? Try our knowledge base or ask our community for more help.
  • Hidden
  • Hidden