Home > Database > Mysql Tutorial > body text

Example analysis of regexp_like() function in mysql

藏色散人
Release: 2019-04-15 11:52:44
Original
24239 people have browsed it

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

If the string matches the provided regular expression, the function returns 1; if it does not match, it returns 0.

Syntax:

REGEXP_LIKE(expr, pat[, match_type])
Copy after login

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 the regular expression.

For example, you can use match_type to specify a case-sensitive match or not.

regexp_like() Example 1 - Basic usage

The following is a basic example:

SELECT REGEXP_LIKE('Cat', '.*') Result;
Copy after login

Result:

+--------+
| Result |
+--------+
|      1 |
+--------+
Copy after login
Copy after login

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

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

SELECT REGEXP_LIKE('Cat', 'b+') Result;
Copy after login

Result:

+--------+
| Result |
+--------+
|      0 |
+--------+
Copy after login
Copy after login

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 0 is returned.

regexp_like() Example 3 - Match the beginning of a string

The following is an example of a regular expression specifying that a string must begin with certain characters:

SELECT REGEXP_LIKE('Cat', '^Ca') Result;
Copy after login

Result:

+--------+
| Result |
+--------+
|      1 |
+--------+
Copy after login
Copy after login

What happens if there is no match:

SELECT REGEXP_LIKE('Cat', '^Da') Result;
Copy after login

Result:

+--------+
| Result |
+--------+
|      0 |
+--------+
Copy after login
Copy after login

regexp_like() example 4 - match_type parameter

You can provide additional parameters to determine the match type. This allows you to specify whether the match is case-sensitive, includes line terminators, etc.

The following 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';
Copy after login

Result:

+----------------+------------------+
| Case-Sensitive | Case-Insensitive |
+----------------+------------------+
|              0 |                1 |
+----------------+------------------+
Copy after login

The match_type parameter can contain the following characters:

c: Case-sensitive matching.

i: Matches regardless of case.

m: Multi-line mode. Identify line terminators in strings. The default behavior is to match line terminators only at the beginning and end of a string expression.

n: The . character matches the line terminator. The default setting is . matches to stop at the end of the line.

u: Unix-only line endings. Only newline characters are recognized as line terminators by the ., ^, and $ matching operators.

Related recommendations: "mysql tutorial"

The above is the detailed content of Example analysis of regexp_like() function in mysql. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template