Home > front end >  Postgres regular expression - get everything before second space
Postgres regular expression - get everything before second space

Time:01-27

I've got query

select(REGEXP_MATCHES('isu_lib.directions_directions_isumapping direction_id bigint null 0', '([^\s] )'))

it shows only first word - isu_lib.directions_directions_isumapping,

but I need 'isu_lib.directions_directions_isumapping direction_id'

Another words - everything before second space.

How to change query? Postgres 11 version

CodePudding user response:

How about just using SUBSTRING():

SELECT SUBSTRING(col FROM '^[^ ]  [^ ]*')
FROM yourTable;

Demo

The regex pattern used here says to match:

  • ^ from the start of the string
  • [^ ] a non space term
  • a single space
  • [^ ]* another non space term, if it is available
  • Related