Get the App
SLTechnology News&Howtos  ›  Database  › 

The usage of regexp_like function in mysql

Shulou Source: shulou.com Published: 2022-06-01 19:58:58 09月19日 Update

The following mainly brings you the usage of the regexp_like function in mysql. I hope the usage of the regexp_like function in mysql can bring you practical use, which is also the main purpose of my editing this article. All right, don't talk too much nonsense, let's just read the following.

In MySQL, the REGEXP_LIKE () function is used to determine whether a string matches a regular expression.

The function returns 1 if the string matches the supplied regular expression, or 0 if it does not match.

Syntax:

REGEXP_LIKE (expr, pat [, match_type])

Where expr is the input string and pat is the regular expression of the test string.

The optional match_type parameter allows you to refine regular expressions.

For example, you can use match_type to specify whether case-sensitive matches or not.

Example 1 of regexp_like ()-basic usage

Here is a basic example:

SELECT REGEXP_LIKE ('Cat','. *') Result

Results:

+-+ | Result | +-+ | 1 | +-+

In this case, the regular expression specifies any character in any sequence, so of course we get a match. The function returns 1 to indicate a match.

Regexp_like () example 2-No match

Here is an example where the input string does not match the regular expression:

SELECT REGEXP_LIKE ('Cat',' baked') Result

Results:

+-+ | Result | +-+ | 0 | +-+

In this case, the regular expression specifies that there should be one or more b characters in any sequence. Our input string does not contain this character, so it returns 0.

Regexp_like () example 3-matches the beginning of a string

Here is an example of a regular expression that specifies that a string must begin with certain characters:

SELECT REGEXP_LIKE ('Cat',' ^ Ca') Result

Results:

+-+ | Result | +-+ | 1 | +-+

What happens if there is no match:

SELECT REGEXP_LIKE ('Cat',' ^ Da') Result

Results:

+-+ | Result | +-+ | 0 | +-+

Regexp_like () example 4-match_type parameter

You can provide additional parameters to determine the type of match. This allows you to specify whether the match is case-sensitive, whether to include line Terminators, and so on.

Here is an example of specifying case-sensitive matching and case-sensitive matching:

SELECT REGEXP_LIKE ('Cat',' ^ ca','c') 'Case-Sensitive', REGEXP_LIKE (' Cat','^ ca','i') 'Case-Insensitive'

Results:

+-+-+ | Case-Sensitive | Case-Insensitive | +-+-+ | 0 | 1 | +-- -+

The match_type parameter can contain the following characters:

C: case-sensitive matching.

I: match regardless of case.

M: multiline mode. Identifies the line Terminator in the string. The default behavior is to match line Terminators only at the beginning and end of a string expression.

N: the. Characters match line Terminator. The default setting is. Match to stop at the end of the line.

U: only at the end of the line of Unix. Only newline characters are recognized as line Terminators by the., ^, and $matching operators.

Correlation

For the above about the use of the regexp_like function in mysql, we do not think it is very helpful. If you need to know more, please continue to follow our industry information. I'm sure you'll like it.

Tags: Characters strings expressions examples regularities functions sizes results parameters input text content sequence beginning on lines only multiple actual nonsense newline characters Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Redmi vpn macOS Huawei