0

I have a table called regions and column region which is not empty. There is a text in it, which is 'Tashkent'. I am trying to access it with expression input. Here is my code:

select * from regions where lower(region) ~ '[t]{1,}[k]{1,}';

As you can see, I am trying to get the word which contains letters t and k consecutively. According to my expression: [t]{1,}[k]{1,} above it should return text 'Tashkent', but not returning. Is my code correct. Can you suggest an alternative code to get word 'Tashkent' knowing that I have letters t and k?

2 Answers 2

1

You need to specify the characters between t and k

select lower('Tashkent') ~ 't{1,}.*k{1,}';
 ?column? 
----------
 t
Sign up to request clarification or add additional context in comments.

3 Comments

Yes, working. Thank you so much. I looked for much sources to know more about POSIX and expressions separately; even documentation of PSQL, but not much there. Can you please suggest any source to lean? Thank you one more time.
@ShohruhAbduakhatov: The term to search for is regular expression
@ShohruhAbduakhatov: Postgresql manual
1

1) If i understand what you want regex is not necessary :

     select * from regions where region ilike '%t%k%'

2) if you want absolutely do this with regex :

  • your request : 't{1,}.*k{1,}' return true only if you have "tk" in your string for example "azerty ttk qwerty".
  • A good solution is Clodoaldo Neto's solution : lower(region) ~ 't{1,}.*k{1,}'

  • You can also remove lower function with region ~ '[tT]{1,}.*[kK]{1,}'

  • and finally {1,} can be replaced by + : region ~ '[tT]+.*[kK]+'

1 Comment

Nice. Good answer. Thanx, much useful things to learn in a single answer. Thanx a lot

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.