2

We have the next SQL query in Postgresql 9.3


SELECT 
  regexp_split_to_array('merchant - mall', '(?!^)\s*(?!\d)[\-]\s*(?!\d)\s*(?=.)') as text1,
  regexp_split_to_array('merchant - street 245', '(?!^)\s*(?!\d)[\-]\s*(?!\d)\s*(?=.)') as text2,
  regexp_split_to_array('merchant - street-245', '(?!^)\s*(?!\d)[\-]\s*(?!\d)\s*(?=.)') as text3,
  regexp_split_to_array('merchant - street - 245', '(?!^)\s*(?!\d)[\-]\s*(?!\d)\s*(?=.)') as text4

The result is:

"{merchant,mall}","{merchant,street 245}","{merchant,street-245}","{merchant,street,245}"

The problem is that 4th sample considers the number as a separate string. Is there any way of doing this with regex in postgresql?

The regex used (in the query) is:

(?!^)\s*(?!\d)[\-]\s*(?!\d)\s*(?=.)

1 Answer 1

2

Just add \s* in your negative lookahead assertion (?!\d):

(?!^)\s*(?<!\d)[\-]\s*(?!\s*\d)\s*(?=.)
          ^              ^^

Or you can use the following simplified regex:

(?<!\d)\b[ -]+\b(?!\d)

See DEMO

Sign up to request clarification or add additional context in comments.

7 Comments

Great! And how would it be if original string will be 123-merchant?
It will be {123,merchant} what are you expecting?
As in 3rd and 4th cases, I expect it to stay with text: { 123-merchant}
Yes, it works properly in Python, but not in postgres where it seems that you can't use lookbehind assertions.
ohh ok... and you can use the above simplified regex inplace of previous one
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.