5

Is there a possibility to find lower and/or upper case letters in QGIS?

I know that it is possible to convert all lower to upper: upper('hello WOrld') → 'HELLO WORLD' and all upper case to lower: lower('HELLO World') → 'hello world', but I can't find just a 'true' or 'false' output.

I have to find all upper and all lower cases to give them a different styling.

1
  • 2
    Need a little clarity. You have texts that are definitely either all upper or all lower case? There's no "Mixed Up" upper and lower case? Or are you trying to style individual letters within a text, for example having M and U in red in "Mixed Up" but everything else blue? – Spacedman Mar 24 '20 at 9:30
10

If the string should contain lowercase, use this expression in 'Expression Dialog'

if(regexp_match("test",'([a-zäöüß]+)'),1,0)

for uppercase change '([a-zäöüß]+)' into '([A-ZÄÖÜ]+)'.

example1

Otherwise, I would recommend writing a small script as was suggested by @MortenSickel.

Personally for me it does not make sense writing a statement if(regexp_match(upper("test"),'([a-z]+)'),1,0) because it will always return 1 or 0 no matter what does your initial string look like.

To get the precise number of lower or upper characters in a string you can use the following code from the 'Function Editor'

from qgis.core import *
from qgis.gui import *

@qgsfunction(args='auto', group='Custom')
def upplow(field, feature, parent):
    upper, lower = [], []
    field_as_list = list(field)
    for i in field_as_list:
        if i.islower():
            lower.append(i)
        elif i.isupper():
            upper.append(i)
        else:
            continue
    return ('Lower: ' + str(len(lower)) + ', Upper: ' + str(len(upper)))

example2


References:

2
  • 2
    The regexp will not work properly if the string contains accented letters...and as the OP is from Germany, he probably wants Ö and Ä as upper case... – MortenSickel Mar 24 '20 at 8:10
  • 1
    thank you for a hint – Taras Mar 24 '20 at 8:17
3

I am pretty sure you would need to do this as a custom function. That makes it more of a general Python question than a GIS question, so it may be better to ask it at Stack Overflow.

But what I would do is to convert the string to upper (or lower) and then go through it letter for letter and take action as needed based on if the letter in my original string equals the corresponding letter in my uppered (or lowered) string.

for (initial, uppered) in zip(mystring, mystring.upper()):
   if initial==uppered:
      # Handle upper case
   else:
      # Handle lower case

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.