Mode Matching Operators
Tags: opengauss
Published: 2023-03-06 09:35:51

There are three separate approaches to mode matching provided by the database: traditional SQL LIKE operator, SIMILAR TO operator, and POSIX-style regular expression. Besides these basic operators, some functions can be used to extract or replace matching substrings and to split a string at matching locations.

**LIKE**

Description: Specifies whether the string matches the mode string following **LIKE**. The **LIKE** expression returns **true** if the string matches the provided mode. (As expected, the **NOT LIKE** expression returns **false** if the **LIKE** expression returns **true**, and vice versa.)

Matching rules:

1. This operator can succeed only when its mode matches the entire string. If you want to match a sequence in any position within the string, the mode must begin and end with a percent sign (%).

2. The underscore (_) represents (matches) any single character. The percent sign (%) indicates the wildcard character of any string.

3. To match a literal underscore or percent sign, the respective character in the mode must be preceded by an escape character. The default escape character is the backslash but a different one can be selected by using the ESCAPE clause.

4. To match escape characters, enter two escape characters. For example, to write a mode constant containing a backslash (\), you need to enter two backslashes in SQL statements.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q81oovy5v6o08sdwkfnj.png)

5. The ILIKE keyword can be used to replace LIKE to make the match case-insensitive.

6. Operator ~~ is equivalent to LIKE, and operator ~~* corresponds to ILIKE.

Example:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l9m1j4h1yqo2z2t4yevs.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tcydx5h04t7nosa00mu7.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2b2ib6ik6x4nj0kql7hl.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pnx4slzush7x9h1rfpiu.png)

**SIMILAR TO**

Description: Returns **true** or **false** depending on whether the mode matches the given string. It is similar to **LIKE**, but differs in that **SIMILAR TO** uses the regular expression understanding mode defined by the SQL standard.

Matching rules:

1. Similar to **LIKE**, this operator succeeds only when its mode matches the entire string. If you want to match a sequence in any position within the string, the mode must begin and end with a percent sign (%).

2. The underscore (_) represents (matches) any single character. The percent sign (%) indicates the wildcard character of any string.

3. **SIMILAR TO** supports these mode-matching metacharacters borrowed from POSIX regular expressions.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dsia12xhjsplg280o1hv.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z3fsqiyekv8ufr5f0l45.png)

4. A preamble escape character disables the special meaning of any of these metacharacters. The rules for using escape characters are the same as those for using **LIKE**.

Example:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ansoswmmfyr8rztiu8tt.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6k0z3gylsyxfi9m7j839.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rzvy0lkt5ledo6rlbaz9.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3ciz8l9t37ui9mwgqeqo.png)

**POSIX Regular Expressions**

Description: A regular expression is a character sequence that is an abbreviated definition of a set of strings (a regular set). If a string is a member of a regular set described by a regular expression, the string matches the regular expression. POSIX regular expressions provide more powerful means for mode matching than the **LIKE** and **SIMILAR TO** operators. Table 1 lists all available operators for mode matching using POSIX regular expressions.

Table 1 Regular expression matching operators

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r0hdw2g1k28gitktuas4.png)

Matching rules:

1. Unlike **LIKE**, a regular expression is allowed to match anywhere within a string, unless the regular expression is explicitly anchored to the beginning or end of the string.

2. Besides the metacharacters mentioned above, POSIX regular expressions also support the following mode matching metacharacters:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/onaod9hinxs1g6gypbv8.png)

Example:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5dlxvw0p1nxuc1phjx6v.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wi8q3p3kzpfq1raamq5v.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fxp6wnif7ie5x93mpd3s.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6zou4q2t43kbtcooikmw.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0cmara77btt6bo9x0l2b.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yk8qnwb2f3lsel7z90pr.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/buec3k333ez9by78fgzj.png)

Although most regular expression searches can be executed quickly, regular expressions can still be artificially made up of memory that takes a long time and any amount of memory. It is not recommended that you accept the regular expression search mode from a non-secure mode source. If you must do this, you are advised to add the statement timeout limit. The **SIMILAR TO** search has the same security risks, as **SIMILAR TO** provides many capabilities that are the same as those of POSIX- style regular expressions. The **LIKE** search is much simpler than the other two options. Therefore, it is more secure to accept the non-secure mode source search.