Skip to main content

Regular Expressions

Regular expressions (regex) in Brinqa Query Language (BQL) enable you to streamline complex queries by combining multiple conditions into a single pattern match.

If you’re new to regex or want to test and understand patterns, tools like regex101 can help you get started. Be sure to select the Java 8 flavor for the most accurate representation.

note

BQL supports the Java flavor of regex. Use the Java 8 regex flavor and the =~ match operator for compatibility.

Recommended regex settings

When querying for strings that follow specific naming conventions, you might end up writing multiple STARTS WITH, ENDS WITH, or CONTAINS conditions. This can quickly become difficult to manage as the conditions grow.

Consider the following BQL query that retrieves hosts with names matching a pattern:

FIND Host AS h WHERE 
h.name STARTS WITH "Alpha" OR
h.name STARTS WITH "Bravo" OR
h.name STARTS WITH "Zebra" AND
h.name CONTAINS ANY ["1", "2", "3"] AND
h.name ENDS WITH "d1234" OR h.name ENDS WITH "n5678"

As the number of name patterns increases, the query becomes harder to read. The same logic can be written as a single regex:

FIND Host AS h WHERE h.name =~ "^[A-Za-z]{5}\\d[dn]\\d{4}$"

Let's breakdown the regex condition:

  • =~: The regex match operator. It is used to indicate that the attribute (e.g., h.name) should be evaluated against the regex pattern that follows.

  • ^: Asserts the start of the string. In this example, the ^ ensures the match begins with exactly five letters, satisfying the [A-Za-z]{5} portion of the pattern.

  • [A-Za-z]{5}: Matches exactly five letters, uppercase, lowercase, or both. This ensures that the retrieved host names start with exactly five letters.

  • \d: Matches a single digit (0-9). This digit must immediately follow the five starting letters of the host name.

  • [dn]: Matches either the letter 'd' or 'n' after the initial five letters and single digit. This provides flexibility for different naming conventions by restricting the next character to either d or n.

  • \d{4}: Matches exactly four digits. These digits must appear directly after the d or n, forming the final part of the host name.

  • $: Asserts the end of the string. This ensures that no additional characters appear after the four digits, enforcing that the string strictly follows the pattern.

    The following screenshot illustrates how the regex accurately matches example test strings to ensure the regex achieves the same results as the lengthy BQL query:

    BQL regex example

Important

While regex is supported in BQL queries, its use is not encouraged. Using regex in BQL can put a strain on the underlying graph database and lead to performance issues.