Regular expressions and patterns in field normalization rules
Summarize
Summary of Regular expressions and patterns in field normalization rules
Field Transformation definitions in ServiceNow allow the use of regular expressions (regex) and pattern matching to identify specific characters or substrings within field values. This capability enables precise manipulation such as replacing, deleting, or inserting characters based on their position in the string during data transformation processes.
Show less
Key Features
- Regex in Transform Parameters and Conditions: Regular expressions can be used both as parameters to locate characters in field values and within condition statements to control when transformations occur.
- Regex Syntax in Parameters: Transform parameters that use regex must begin with
/regex/, followed by the regular expression pattern that identifies the target characters. - Example Use Case: Transforming a computer name field formatted as
domain\machinenameto retain only the machine name by removing the domain and backslash.
Practical Application Example
A network administrator wants to simplify computer names from the format development\devlab01 to just devlab01. Using a field transformation on the Name field of the cmdbcicomputer table, the following regex-based transformation is applied:
- Find:
/regex/.\\(.)— This matches the entire original string, where.captures all characters before the backslash, and(.)captures the machine name after the backslash. - Replace with:
$1— This refers to the group captured after the backslash, effectively removing the domain and backslash.
The transformation is tested within the transformation record, confirming that input development\devlab01 is transformed to devlab01. Once validated, the transformation mode is set to Active, and the Transformation application data job is run to apply this to existing records.
Benefits for ServiceNow Customers
- Enables automated, precise cleaning and normalization of field data using regex patterns.
- Simplifies complex string manipulations, such as removing prefixes or domain identifiers from field values.
- Supports bulk application of transformations to existing data through the Transformation application data job.
- Improves data consistency and usability within ServiceNow CMDB and other tables by standardizing values at the field level.
Field Transformation definitions support the use of regular expressions (referred to in the platform as regex) and pattern matching for determining the position of characters in a string.
After identifying the target characters, field transformation can replace or delete the identified characters or insert other characters at that position.
Regex
Regular expressions can be used in transform parameters and in condition statements to determine which characters in a field value are transformed.
Regular expressions used as parameters to locate characters in transformed field values must begin with /regex/. Everything after that is a regular expression that is used to calculate character position.
Example
The computer names in an organization's Windows network are expressed as domain\machine name, such as development\devlab01. The network administrator wants to simplify these names by removing the domain name and backslash. He creates a transformation record for the Computer [cmdb_ci_computer] table and selects the Name field to transform.
- Find:/regex/.*\\(.*)
- Replace with:$1
The regular expression .*\\(.*) represents the entire raw value in the Name field - in this example development\devlab01. The first part of the expression, .*, represents everything before the backslash (the development domain name). The backslash by itself is the escape character in regular expressions and requires special syntax to retain its function in the computer name. The administrator must escape it by using another backslash (\\ means \). The part of the expression after the backslash, (.*), represents the computer name (devlab01) and is grouped within parentheses for reference. The value in the Replace with field, $1, references this group and replaces the entire raw value of the field with the contents of the group, devlab01.
The administrator clicks Test transforms in the transformation record and enters development\devlab01 in the Raw data field. He then clicks OK to apply the transform to the test value. The transform replaces development\devlab01 with devlab01.
When the transforms for this field are tested successfully, the administrator changes the Mode in the transformation record to Active and runs the Transformation application data job to apply this transformation to existing records in the database.