0

I want to extract,

002267b4-ad06-11e4-89ca-59f94b49bbc0

the above string from another string. What I have tried is,

select regexp_matches('class 1 type 1 cat 1 002267b4-ad06-11e4-89ca-59f94b49bbc0' , '\b\w{1,8}\-\w{1,4}\-\w{1,4}\-\w{1,4}\-\w{1,12}\b' ) 

This does not give any result, BUT when I check the regular expression from an online tool it selects the correct string I want. Must be an issue in the postgresql query.

Please help.

7
  • Put the pattern inside () Commented Apr 7, 2015 at 12:13
  • 1
    \b is a shortcut for "backspace" - I don't think this is what you want. I also don't think you need to escape the - outside of a range operator ([..]). I think '\w{1,8}-\w{1,4}-\w{1,4}-\w{1,4}-\w{1,12}' should do it Commented Apr 7, 2015 at 12:14
  • 1
    Try [[:<:]] and [[:>:]] instead of first and last \b. See postgresql.org/docs/8.3/interactive/…. Commented Apr 7, 2015 at 12:22
  • @hjpotter92 But posgres says no brackets tutorialspoint.com/postgresql/postgresql_string_functions.htm Commented Apr 7, 2015 at 12:23
  • 1
    Ok, try \m for the first \b and \M for the last \b: \m\w{1,8}\-\w{1,4}\-\w{1,4}\-\w{1,4}\-\w{1,12}\M. \b means a backspace in postgresql regex flavor. Commented Apr 7, 2015 at 12:24

2 Answers 2

2

Accoding to PostgreSQL regex documentation, you need to use \m for a beginning of a word, and \M for the end of the word boundaries. \b means a backspace. So, your regex should lool like:

select regexp_matches('class 1 type 1 cat 1 002267b4-ad06-11e4-89ca-59f94b49bbc0' , '\m\w{1,8}\-\w{1,4}\-\w{1,4}\-\w{1,4}\-\w{1,12}\M' ) 
Sign up to request clarification or add additional context in comments.

Comments

1

Try this query:

SELECT regexp_matches( 'class 1 type 1 cat 1 002267b4-ad06-11e4-89ca-59f94b49bbc0', '[^-]{1,8}-[^-]{1,4}-[^-]{1,4}-[^-]{1,4}-[^-]{1,12}' ); 

This can be Fiddel around with

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.