Z.Chorlev
Z.Chorlev

Reputation: 159

Pattern for matching letters and digits in mysql table

I can't find any pattern for matching letters and numbers in sql query.

[PG]\d+.* - This is the pattern for the query I've tried some things but i only match letter or digit not both.

 SELECT * FROM cars WHERE REGEXP_LIKE(model, '^[PG]\d');

It must match everything that starts with p or g followed by digits and ends with some letters. Something like this p01929ki, g9102rt

Upvotes: 2

Views: 1108

Answers (2)

Primit
Primit

Reputation: 865

Try this one

 select * from cars where regexp_like(model,'^[pg][0-9]+[[a-zA-z]+$')

Upvotes: 1

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626699

You may consider a pattern like

WHERE REGEXP_LIKE(model, '^[pg][0-9]+[[:alpha:]]*$')

See the regex demo.

Details

  • ^ - string start
  • [pg] - p or g
  • [0-9]+ - 1+ digits
  • [[:alpha:]]* - 0+ letters
  • $ - end of string.

Upvotes: 4

Related Questions