chaitanya89
chaitanya89

Reputation: 847

Regex to check if a string has only spaces

I want a regular expression to check if a string has only spaces.

Example: " "

I'm currently using this regex

[/^ *$/]

But it's also detecting a string which has words in it.

Example: "abc xyz"

I want to use this regex in a postgresql function like following

IF r.colmn IS NULL  OR CAST(r.colmn as text) = '' 
       OR CAST(r.colmn as text) ~ '[/^ *$/]' -- regex not working
THEN
     RAISE NOTICE 'Do something';       
END IF;

Is there any regex I can use in a postgresql function which only checks for a string with only spaces?

Upvotes: 5

Views: 4000

Answers (2)

Dorian
Dorian

Reputation: 9145

For instance to find non-empty notes:

SELECT "notes".*
FROM "notes"
WHERE
  "notes"."body" IS NOT NULL
  AND
  NOT (body ~* '^\s*$')

Upvotes: 0

anubhava
anubhava

Reputation: 785561

You need to remove [/ and /] from your regex:

IF r.colmn IS NULL  OR CAST(r.colmn as text) = '' 
       OR CAST(r.colmn as text) ~ '^ *$'
THEN
     RAISE NOTICE 'Do something';       
END IF;

Upvotes: 4

Related Questions