All Collections
Data Transformation
Custom Dimensions
How do I use regex to capture a fixed amount of characters from dimension values?
How do I use regex to capture a fixed amount of characters from dimension values?

Learn how to specify a certain number of characters to be returned from native dimensions as values in a custom dimension

Simon Kalicinski avatar
Written by Simon Kalicinski
Updated over a week ago

In some cases, you may want to "cut off" certain dimension values from your data sources as you map them into one of your custom dimensions. This process can also be referred to as truncating. In this particular example, we're going to essentially shorten the dimension value we want to map to our custom dimension to only be (at most) 20 characters long. There are more advanced applications of this type of regex, but this is a basic but not at all uncommon use case.

  1. First, edit your custom dimension and create a new rule.

  2. Choose which other dimension you would like to capture values from and use in your custom dimension.

  3. Change the condition of WHEN from "exactly matches" to "matches regex".

  4. In the input field of WHEN, enter the following regex: ^(.{20}).*$ 

  5. Make sure that, for THEN [DIMENSION NAME] IS, you choose "regex match" and "capture group 1".

You're all done!

NOTE: here is some clarification on the regex characters used:

  • ^ denotes the start of a string

  • () defines a capture group

  • . denotes any character, a "wildcard" character

  • {} defines an amount of characters to match against (usually only used for 2 or more finite numbers of characters)

  • * denotes any amount of characters to be matches against

  • $ denotes the end of a string

Did this answer your question?