skip to Main Content

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

2

Answers


  1. 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]+';
    
    Login or Signup to reply.
  2. Simply remove black spaces from regex:

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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search