# Mode Matching Operators

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.
    
    > **NOTE:** When **standard\_conforming\_strings** is set to **off**, any backslashes you write in literal string constants will need to be doubled. So, writing a mode that matches a single backslash actually involves writing four backslashes in the statement (you can avoid this by selecting a different escape character with the **ESCAPE** clause so that the backslash is no longer a special character of **LIKE**. But the backslash is still a special character of the character text analyzer, so you still need two backslashes.) In a MySQL-compatible mode, it is also possible to select no escape character by writing **ESCAPE ''**. This effectively disables the escape mechanism, which makes it impossible to turn off the special meaning of underscores and percent signs in the mode.
    
5. The **ILIKE** keyword can be used to replace **LIKE** to make the match case-insensitive.
    
6. Operator <s>is equivalent to </s> **<s>LIKE</s>**<s>, and operator </s> \* corresponds to **ILIKE**.
    

Example:

```plaintext
""openGauss=# SELECT 'abc' LIKE 'abc' AS RESULT;
 result
-----------
 t
(1 row)
```

```plaintext
""openGauss=# SELECT 'abc' LIKE 'a%' AS RESULT;
 result
-----------
 t
(1 row)
```

```plaintext
""openGauss=# SELECT 'abc' LIKE '_b_' AS RESULT;
 result
-----------
 t
(1 row)
```

```plaintext
""openGauss=# SELECT 'abc' LIKE 'c' AS RESULT;
 result
-----------
 f
(1 row)
```

## **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.
    
    | **Metacharacter** | **Description** |
    | --- | --- |
    | | | Specifies alternation (either of two alternatives). |
    | \* | Specifies that the previous item is repeated for zero or more times. |
    | + | Specifies that the previous item is repeated for one or more times. |
    | ? | Specifies that the previous item is repeated for zero or one time. |
    | {m} | Specifies that the previous item is repeated for *m* times. |
    | {m,} | Specifies that the previous item is repeated for *m* or more times. |
    | {m,n} | Specifies that the previous item is repeated for at least *m* times and at most *n* times. |
    | () | Specifies that parentheses () can be used to group items into a single logical item. |
    | \[...\] | Specifies a character class, just as in POSIX regular expressions. |
    
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:

```plaintext
""openGauss=# SELECT 'abc' SIMILAR TO 'abc' AS RESULT;
 result
-----------
 t
(1 row)
```

```plaintext
""openGauss=# SELECT 'abc' SIMILAR TO 'a' AS RESULT;
 result
-----------
 f
(1 row)
```

```plaintext
""openGauss=# SELECT 'abc' SIMILAR TO '%(b|d)%' AS RESULT;
 result
-----------
 t
(1 row)
```

```plaintext
""openGauss=# SELECT 'abc' SIMILAR TO '(b|c)%'  AS RESULT;
 result
-----------
 f
(1 row)
```

## **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](https://docs.opengauss.org/en/docs/latest/docs/BriefTutorial/mode-matching-operators.html#en-us_topic_0283137455_en-us_topic_0237121970_table6512684711360) lists all available operators for mode matching using POSIX regular expressions.

**Table 1** Regular expression matching operators

| **Operator** | **Description** | **Example** |
| --- | --- | --- |
| ~ | Matches a regular expression, which is case-sensitive. | 'thomas' ~ '.\*thomas.\*' |
| ~\* | Matches a regular expression, which is case-insensitive. | 'thomas' ~\* '.\*Thomas.\*' |
| !~ | Does not match a regular expression, which is case-sensitive. | 'thomas' !~ '.\*Thomas.\*' |
| !~\* | Does not match a regular expression, which is case-sensitive. | 'thomas' !~\* '.\*vadim.\*' |

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:
    
    | **Metacharacter** | **Description** |
    | --- | --- |
    | ^ | Specifies the match starting with a string. |
    | $ | Specifies the match at the end of a string. |
    | . | Matches any single character. |
    

Example:

```plaintext
""openGauss=#  SELECT 'abc' ~ 'Abc' AS RESULT;
result 
--------
 f
(1 row)
```

```plaintext
""openGauss=# SELECT 'abc' ~* 'Abc' AS RESULT;
 result 
--------
 t
(1 row)
```

```plaintext
""openGauss=# SELECT 'abc' !~ 'Abc' AS RESULT;
 result 
--------
 t
(1 row)
```

```plaintext
""openGauss=# SELECT 'abc'!~* 'Abc' AS RESULT;
 result 
--------
 f
(1 row)
```

```plaintext
""openGauss=# SELECT 'abc' ~ '^a' AS RESULT;
 result 
--------
 t
(1 row)
```

```plaintext
""openGauss=# SELECT 'abc' ~ '(b|d)'AS RESULT;
 result 
--------
 t
(1 row)
```

```plaintext
""openGauss=# SELECT 'abc' ~ '^(b|c)'AS RESULT;
 result 
--------
 f
(1 row)
```

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.

[  
](https://docs.opengauss.org/en/docs/latest/docs/BriefTutorial/aggregate-functions.html)
