13

The documentation says that \s is whitespace and \S is not whitespace. So far, nothing new to regex users.

But let's check some return values:

SELECT SUBSTRING('abc a c' FROM 'a\\sc');
'a c'

SELECT SUBSTRING('abc a c' FROM 'a[\\s]c'); -- Note the character class
'a c'

SELECT SUBSTRING('abc a c' FROM 'a\\Sc');
'abc'

SELECT SUBSTRING('abc a c' FROM 'a[\\S]c'); -- Note the character class
ERROR:  invalid regular expression: invalid escape \ sequence

So it seems, \s can be used in a character class and \S cannot. Why?

1 Answer 1

9

From the manual:

Within bracket expressions, \d, \s, and \w lose their outer brackets, and \D, \S, and \W are illegal.

In any case, the brackets seem redundant since \s and \S themselves are character classes.

The following syntax works for me as an alternative to a[\\S]c:

SELECT SUBSTRING('abc a c' FROM 'a[^[:space:]]c');
2
  • Thx, I overlooked this. BTW, of course the brackets are redundant in my simplified example code.
    – Leif
    Sep 23, 2011 at 8:59
  • The reason they are illegal is because a[^\s]c works just fine for negation.
    – hhaamu
    Sep 23, 2011 at 11:25

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.