A nice QGIS question!
You can achieve this using the Field Calculator in QGIS, with a bit of regex magic. Here's the expression you'll need:
Update SLAD2 with:
lower(regexp_replace("SLAD", '([^0-9])([A-ZĄĆĘŁŃÓŚŹŻ])', '\\1' || lower('\\2')))
Let's break it down:
regexp_replace: This function replaces substrings matching a regular expression pattern.
"SLAD": The input string (your original column).
([^0-9])([A-ZĄĆĘŁŃÓŚŹŻ]): The regex pattern. It matches:
([^0-9]): Any character that is not a digit (captured in group 1, referred to as \\1 later). This ensures we don't convert letters after digits.
([A-ZĄĆĘŁŃÓŚŹŻ]): Any uppercase letter (including Polish characters). Captured in group 2 (\\2).
'\\1' || lower('\\2'): The replacement string.
\\1 refers to the first captured group (the non-digit character before the letter). We keep it as is.
lower('\\2'): Converts the second captured group (the uppercase letter) to lowercase using the lower() function.
|| is the string concatenation operator.
How it works:
- If the current character is an uppercase letter (including Polish chars) and the previous character is not a digit, the regex matches and replaces the letter with its lowercase equivalent.
- If the previous character is a digit, the regex doesn't match, and the letter remains unchanged (i.e., stays uppercase).
lower() then converts any remaining uppercase letters (not preceded by a non-digit) to lowercase.
Example:
Suppose SLAD contains: Abc1ĆółŃÓŁąż
A (pos 1): prev char doesn't exist, so convert to a
b (pos 2): prev char a is not a digit, so convert to b (already lowercase, no change)
c (pos 3): prev char b is not a digit, so convert to c (already lowercase, no change)
1 (pos 4): digit, skip
Ć (pos 5): prev char 1 is a digit, don't convert, stays Ć
ółŃÓŁąż... you get the idea
Result in SLAD2: abc1ćółńółąż
In QGIS Field Calculator:
- Open the attribute table of your layer.
- Enable editing (toggle editing button or Ctrl+E).
- Open Field Calculator (abacus icon or Ctrl+I).
- Select
Update existing field and choose SLAD2 from the dropdown.
- Paste the expression above into the editor.
- Click OK.
Voilà! Your SLAD2 column should now contain the modified values.
Tested in QGIS 3.16, but should work in earlier versions too. If you encounter issues, double-check the Polish characters are correctly represented in the regex pattern. Happy QGISing!