How match substring ignoring case in erlang

Viewed 263

How I can find in ETS table by a string that ignore upper/lower case?

This code find by a string starting with wo:

ets:test_ms({"WORD"}, [{{"wo"++'_'}, [], ['$_']}])

But, like WORD is upper case, nothing is returned.

Thanks!

1 Answers

There is not a support for the case-insensitive matching in match spec (or matching in Erlang generally). You have basically three options there.

  1. Generate all cases

    [{{"wo"++'_'}, [], ['$_']}, {{"wO"++'_'}, [], ['$_']}, {{"Wo"++'_'}, [], ['$_']}, {{"WO"++'_'}, [], ['$_']}]
    
  2. Use guard expression (for longer words because the number of combinations is 2^N)

    [{{['$1','$2'|'_']},
      [{'orelse',{'=:=','$1',$w},{'=:=','$1',$W}},
       {'orelse',{'=:=','$2',$o},{'=:=','$2',$O}}],
      ['$_']}]
    
  3. Transform data in ets table using string:casefold/1 and search by this key.

Related