Home > Mobile >  How to find text pattern in string for postgresql SQL
How to find text pattern in string for postgresql SQL

Time:10-30

For this sample string: "... Key Match extra text..."

How do I get the value "Match", which is the string between blank spaces after "Key"?

is there a better way than: Find position of "Key "->pos1, find position of first blank space after p1 -> p2, substring(string, p1,p2)?

This is not working as I expected

Select substring('Key Match extra text', 'Key (. ) ');
---
Match extra

CodePudding user response:

You can make the regex be "non-greedy", so that . matches as few as possible:

Select substring('Key Match extra text', 'Key (. ?) ');

Or you can change . to something that won't match spaces:

Select substring('Key Match extra text', 'Key (\S ) ');
  • Related