Home > Software engineering >  Use REGEXP_REPLACE to generate slug value with numbers
Use REGEXP_REPLACE to generate slug value with numbers

Time:12-17

I am using a function REGEXP_REPLACE within SQL to generate a slug value from name column

UPDATE tag
       SET slug = LOWER(REGEXP_REPLACE(
                REGEXP_REPLACE('Team Building 2022', '^[^A-Za-z] |[^A-Za-z] $', '', 'g'),
                 '[^A-Za-z] |(?<=[a-z])(?=[A-Z])', '-', 'g')
         );

But I get: team-building

Result I want is: team-building-2022

Is there a way to add numbers in this function? Thanks

CodePudding user response:

Well you did not actually specify what result you want. but from the actual question and the reply to @WiktorStribiżew it appears like you are wanting team-building-2022. If that is correct then you just need a simple replace() function (table 9.10) rather than a complicated regular expression. So just:

select lower(replace('Team Building 2022',' ', '-'));

If that is not the desired result then please post the exact result you want.

CodePudding user response:

Resolved it with:

LOWER(REGEXP_REPLACE(
      REGEXP_REPLACE(name, '^[^A-Za-z0-9] |[^A-Za-z0-9] $', '', 'g'),
         '[^A-Za-z0-9] |(?<=[a-z])(?=[A-Z])', '-', 'g')
  • Related