qgis – Filtering Lists with Mixed Capital and Small Letters in QGIS

expressionfilterqgisregular expressiontitle

My dataset contains mixed values of both capital and small letter wordings, e.g.

  1. Hello world (contains fully small letter wording 'world')
  2. HELLO World (contains fully capital letter wording 'HELLO')

I tried to "title" the column but I identified some values which should be in the full capitalized form are changing e.g.:

  1. APOLLO changes to Apollo
  2. KFC changes to kfc

If I can filter the data which contains both fully Capital letter words and full small letter words it helps me to change the required data.

Best Answer

Use the following expression to create an array for each word of the input string, then check each element of the array (each word separately) if it is in UPPERCASE only (if it contains more than one uppercase characters) and if so, convert it to Title case (first character in uppercase), otherwise leave it as it is. Than covert back the array to a string.

In line 5, the part ([A-Z]{2,1000}) defines a word consisting of at least 2 and max. 1000 UPPERCASE characters. Based on what exactly you want to do, you can add/modify conditions, e.g. for lowercase only words. But for this, provide more information.

array_to_string(
    array_foreach (
        string_to_array( text, delimiter:=' '),
        case 
            when regexp_matches( @element, '([A-Z]{2,1000})') is not NULL
            then title (@element)
            else @element
        end
    ),
    delimiter:=' '
)

The expression converts all words with two or more characters in UPPERCASE to Title case: enter image description here

Related Question