Home > Database > Mysql Tutorial > body text

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

Susan Sarandon
Release: 2024-10-26 13:01:29
Original
702 people have browsed it

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>
Copy after login

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
Copy after login
Copy after login

The above query will return:

trecord
-------
ALA0000
ALA0001
ALA0002
Copy after login
Copy after login

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!

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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!