'Pattern Matching in Redshift

In my SQL query, I want to apply a match pattern similar to this

"AB_"[Only Letters(Can be 0 to n number of Letters)]

To explain, The First 3 strings "AB_" are fixed and rest can be 0 to 'n' number of letters only.

For example the desired output should be

  1. AB_abc
  2. AB_asdehfsdj
  3. AB_

and many similar patterns like this.

What would be the best way to achieve this?



Solution 1:[1]

the regex for that would be ^AB_[a-zA-Z]{0,n}$, where n is a positive integer

so, with n == 5, your query could be written as

select my_col 
from my_table 
where my_col ~ '^AB_[a-zA-Z]{0,5}$'

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Haleemur Ali