Home  >  Article  >  Database  >  How to Match Characters Followed by Digits in MySQL using REGEXP?

How to Match Characters Followed by Digits in MySQL using REGEXP?

Susan Sarandon
Susan SarandonOriginal
2024-10-26 13:01:29704browse

How to Match Characters Followed by Digits in MySQL using REGEXP?

Regex Query in MySQL

In MySQL, the LIKE operator is commonly used for pattern matching. However, when attempting to match characters followed by digits, it often results in null values. To overcome this, consider utilizing the REGEXP operator instead.

The following query targets records beginning with the character sequence "ALA" and ending with a single digit:

<code class="mysql">SELECT trecord FROM `tbl` WHERE (trecord REGEXP '^ALA[0-9]')</code>

The "^" symbol ensures that the matching starts at the beginning of the record, while "[0-9]" specifies that the last character should be a digit.

For example, if the table contains the records:

trecord
-------
ALA0000
ALA0001
ALA0002

The above query will return:

trecord
-------
ALA0000
ALA0001
ALA0002

By employing REGEXP, you can enhance the expressiveness of your MySQL queries for pattern matching scenarios involving specific character sequences followed by digits.

The above is the detailed content of How to Match Characters Followed by Digits in MySQL using REGEXP?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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