Skip to content
Developerhome
Sage Distribution and Manufacturing Operations

Regular expressions

  Less than to read

A regular expression is a sequence of symbols and characters that represent a string or pattern searched within a longer piece of text.

_regex restricts the records that may be impacted to the ones where the regular expression exists.

Regular expressions are expressed in a string.

The following tables displays examples and results of applying filters with a regular expression. Some of the fields displayed are not mandatory and others have been omitted.

{
  xtremMasterData {
    item {
      query(filter: "{name: {_regex: 'C.*a'}}") {
        edges {
          node {
            id
            name
          }
        }
      }
    }
  }
}
{
    "data": {
        "xtremMasterData": {
            "item": {
                "query": {
                    "edges": [
                        {
                            "node": {
                                "id": "001",
                                "name": "Coffee Beans"
                            }
                        },
                        {
                            "node": {
                                "id": "101",
                                "name": "TestCottage Pie"
                            }
                        },
                        {
                            "node": {
                                "id": "Cable1",
                                "name": "Cable 1"
                            }
                        },
                        {
                            "node": {
                                "id": "Cable2",
                                "name": "Cable 2`"
                            }
                        }
                    ]
                }
            }
        }
    }
}
{
  xtremMasterData {
    item {
      query(filter: "{name:{_not: {_regex: 'C.*a'}}}") {
        edges {
          node {
            id
            name
          }
        }
      }
    }
  }
}
{
    "data": {
        "xtremMasterData": {
            "item": {
                "query": {
                    "edges": [
                        {
                            "node": {
                                "id": "10",
                                "name": "myTest"
                            }
                        },
                        {
                            "node": {
                                "id": "1012",
                                "name": "onion"
                            }
                        },
                        {
                            "node": {
                                "id": "1013",
                                "name": "mince"
                            }
                        },
                        {
                            "node": {
                                "id": "102",
                                "name": "potato mash"
                            }
                        }
                    ]
                }
            }
        }
    }
}

The following table displays the different cases of regular expressions extracted from PostgreSQL documentation:

**
Metacharacter matching a single character Matches Examples
. Dot Matches any character.
[...] Character class Matches any character inside the brackets. **[abc]** matches "**a**", **b**, and **c**.
[^...] Negated character class Matches any character except those inside the brackets. **[^abc]** matches all characters except **a**, **b**, and **c**.
- Dash Used within a character class to indicate a range of characters. **[2-7]** matches any digits between **2** and **7**.
**[0-3a-d]** is equivalent to **[0123abcd]**.
\ Escaped character Interpret the next character literally. **3\.14** matches only **3.14** whereas **3.14** matches **3214**, **3.14**, **3z14**, and so on.
\\xnn Binary character Match a single binary character where nn is a hexadecimal value between 00 and FF. **\\x41** matches **A**.
Metacharacters as quantifiers Matches Examples
? Question The preceding expression once or not at all. **colou?r** matches **colour** or **color**.
**[0-3][0-5]?** matches **2** and **25**.
* Star Zero or more occurrences of the preceding set of characters. .* Zero or more occurrences of any character.
+ Plus One required, additional are optional. **[0-9]+** matches **1**, **15**, **220**, and so on.
??, +?, *? Non-greedy versions of ?, + and *. Match as little as possible, whereas the greedy versions match as much as possible. **[content]<.*?>** matches the **<.*>[content]** input string.
Metacharacters as position markers Matches Examples
^ Caret At the end of a regular expression, this character matches the end of the input. **^2** will only match input that begins with **2**.
**^[45]** will only match inputs that begin with **4** or **5**.
$ Dollar At the end of a regular expression, this character matches the end of the input. >**$** matches a **>** at the end of the input.
Other metacharacters Matches Examples
| Alternation Matches either expression it separates. **H|Cat** matches **Hat** or **Cat**.
(...) Parenthesis Provides grouping for quantifiers, limits scope of alternation via precedence. **(abc)*** matches 0 or more occurrences of the **abc** string.
**WhatsUp (Gold)|(Professional)** matches **WhatsUp Gold** or **WhatsUp Professional**.
\0, \1, ... Backreference Matches text previously matched within the first, second, and following match groups (starting group). **<{head}>.*?</\0>** matches **xxx**.
! Negation The following expression does not match the input. **a!b** matches **a** not followed by **b**.