Home > OS >  postgres like - matching any 1 or 0 chars
postgres like - matching any 1 or 0 chars

Time:01-11

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 ...

CodePudding user response:

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)
  • Related