Home > Blockchain >  How to make a regular expression for this format in MySQL
How to make a regular expression for this format in MySQL

Time:08-01

I want to make a regular expression for strings like this in MySQL:

192.135.135, 28.215.217, 1.352.213

How could I do this?

CodePudding user response:

You can use the regular expression pattern ^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}$.

For example:

select s, s regexp '^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}$' as matches
from (
  select '192.135.135' as s
  union all select '28.215.217'
  union all select '1.352.213'
  union all select '1.2'
  union all select '1.2.3.4'
  union all select 'Hello World'
) x

Returns:

 s            matches 
 ------------ ------- 
 192.135.135  1       
 28.215.217   1       
 1.352.213    1       
 1.2          0       
 1.2.3.4      0       
 Hello World  0       

See running example at db<>fiddle.

  • Related