How to search if whole word exists in a String in Postgres

后端 未结 3 1264
天命终不由人
天命终不由人 2021-01-25 14:46

I have a table with a column field that has values like Samsung Phone.

My question is how can I get this row if I search for a string \"Samsung\" or \"phone

3条回答
  •  一向
    一向 (楼主)
    2021-01-25 15:20

    Also you can use this:

    title ~* '(^|[^\w])samsung([^\w]|$)'
    

    The advantage of the above one is, it can be easily extended to include characters from different encoding like this: (Turkish characters)

    title ~* '(^|[^\wğüşıöçĞÜŞİÖÇ])samsung([^\wğüşıöçĞÜŞİÖÇ]|$)
    

    Here is a sample case where you may need the above extension.
    For example; in a Latin5 encoded database you have a value of 'İsamsung'. İ is the capital of i in Turkish.
    In this case the title ~* '(\msamsung\M)' does not work. This criteria returns the İsamsung value. Because in Latin5 encoding, postgre thinks İ is out of alpanumeric range and the value is something similiar to :samsung.

提交回复
热议问题