Home > Blockchain >  MySQL REGEXP That Will Detect Numbers Separated by a Space
MySQL REGEXP That Will Detect Numbers Separated by a Space

Time:10-01

I'm trying to create a REGEXP pattern that will check if a string contains a set of numbers separated by a space. I have tried the 4 statements below. They all return 0. What am I doing wrong? Thanks.

SELECT '0630 3062' REGEXP '^[0-9]{4} [[:space:]] [0-9]{4}$';

SELECT 'Employee 346 retrad: 0630 3062' REGEXP '[0-9]{4} [[:space:]] [0-9]{4}';

SELECT 'Employee 346 retrad: 0630 3062' REGEXP '[0-9]  [[:space:]] [0-9] ';

SELECT 'Employee 346 retrad: 0630 3062 1657 in July Assessment' REGEXP '[0-9]  [[:space:]] [0-9] ';

The statements are also in this Fiddle

I have looked at a similar post/thread and the two answers there don't work; the query still returns 0 instead of 1. Thanks

CodePudding user response:

You are actually matching 2 times a space and a single [[:space:]] in the middle.

You want to match only a single [[:space:]]

SELECT '0630 3062' REGEXP '^[0-9]{4}[[:space:]][0-9]{4}$';

SELECT 'Employee 346 retrad: 0630 3062' REGEXP '[0-9]{4}[[:space:]][0-9]{4}';

SELECT 'Employee 346 retrad: 0630 3062' REGEXP '[0-9] [[:space:]][0-9] ';

SELECT 'Employee 346 retrad: 0630 3062 1657 in July Assessment' REGEXP '[0-9] [[:space:]][0-9] ';

CodePudding user response:

Simply remove black spaces from regex:

^[0-9]{4}[[:space:]][0-9]{4}$ returns 1 in first statement

  • Related