Joey Baruch
Joey Baruch

Reputation: 5209

postgres like - matching any 1 or 0 chars

I'm reading the postgres docs on the like keyword, and I see:

An underscore (_) in pattern stands for (matches) any single character; a percent sign (%) matches any sequence of zero or more characters.

Is there any way to match any single or no characters?

Examples:

For the purpose of the examples, I'm using the char as the operator I'm looking for:

like 'a∆b':
'ab' - > True
'acb' -> True
'a-b' -> True
'a.b' -> True
'a..b' -> False
'ba' -> False ...

Upvotes: 0

Views: 411

Answers (1)

user330315
user330315

Reputation:

You need a regular expression for that:

where the_column ~ '^a.{0,1}b$'

The regex means:

  • starts with a (^ anchors at the start)
  • followed by zero or one character (. matches any character, {0,1} is zero or one repetitions)
  • and ends with b ($anchors at the end)

Upvotes: 3

Related Questions