DEV Community

Abhi-Kmr2046
Abhi-Kmr2046

Posted on

Pattern Matching In Apache AGE

Pattern matching is one of the most fundamental operation in a database system. It has numerous use cases like validating form submissions, performing search and replace operations, cleaning and standardizing data, and searching for specific patterns in a database.
Functions provided by Apache AGE for this are as follows:

Starts With

Performs case-sensitive prefix searching on strings.

SELECT * FROM cypher('graph_name', $$
    MATCH (v:Person)
    WHERE v.name STARTS WITH "J"
    RETURN v.name
$$) AS (names agtype);
Enter fullscreen mode Exit fullscreen mode

Contains

Performs case-sensitive inclusion searching in strings.

SELECT * FROM cypher('graph_name', $$
    MATCH (v:Person)
    WHERE v.name CONTAINS "o"
    RETURN v.name
$$) AS (names agtype);
Enter fullscreen mode Exit fullscreen mode

Ends With

Performs case-sensitive suffix searching on strings.

SELECT * FROM cypher('graph_name', $$
    MATCH (v:Person)
    WHERE v.name ENDS WITH "n"
    RETURN v.name
$$) AS (names agtype);

Enter fullscreen mode Exit fullscreen mode

Regular Expressions

Regex or regular expressions are special sequences used to find or match patterns in strings. AGE supports the use of POSIX regular expressions using the =~ operator.
Example:

SELECT * FROM cypher('graph_name', $$
    MATCH (v:Person)
    WHERE v.name =~ '(?i)JoHn'
    RETURN v.name
$$) AS (names agtype);
Enter fullscreen mode Exit fullscreen mode

We can provide any regular expression after ~= and it will search for the strings that matches that expression.

The special characters used in regex include the backslash , the caret ^, the dollar sign $, the period or dot ., the vertical bar or pipe symbol |, the question mark ?, the asterisk or star *, the plus sign +, the opening parenthesis (, the closing parenthesis ), the opening square bracket [, and the opening curly brace {. These special characters are often called “metacharacters”.

The backslash \ is used to escape metacharacters so they can be treated as literal characters. The caret ^ is used to match the start of a string. The dollar sign $ is used to match the end of a string. The period or dot . is used to match any character except newline. The vertical bar or pipe symbol | is used to match either/or. The question mark ? is used to match zero or one of the preceding character. The asterisk or star * is used to match zero or more of the preceding character. The plus sign + is used to match one or more of the preceding character. The opening parenthesis ( and closing parenthesis ) are used for grouping. The opening square bracket [ and closing square bracket ] are used for character classes. The opening curly brace { and closing curly brace } are used for quantifiers.

Top comments (0)