I need to get the unique values from a field.
(?<=,|^)([^,]*)(,1円)+(?=,|$)
Works in an online test but I get invalid expression in QGIS Field Calculator. How can I rewrite this in QGIS Regex implementation or is there another maybe pythonic way to get the same result? In the end it needs to work in QGIS Modeller.
regexp_matches( "Locality_L","(?<=,|^)([^,]*)(,1円)+(?=,|$)")
Update 2 As per @she_weeds suggestion I tried with '' around the regex statement, it runs but there's no value in the table even though a sample result is shown in the field calc pane. enter image description here
2 Answers 2
Instead of using regex_match you can define a new function and use that custom function to extract the unique text without using regular expression.
You can use the following function to do the job:
from qgis.core import *
from qgis.gui import *
@qgsfunction(args='auto', group='Custom')
def unique_text(text_field, feature, parent):
split_text = text_field.split(',')
lower = [txt.lower().strip() for txt in split_text]
uniqueText = set(lower)
joinUnique = (",".join(uniqueText)).title()
return joinUnique
Using Field Calculator ->
go to Function Editor
and create a new expression and give it a name, in this example ExtractUniqueText
Under the Field Calculator -> Custom
use Unique_text which is the name of the defined function with the field name that you want to extract the unique text from.
Here is the output:
-
Thanks! Custom function works well. unfortunately array_distinct() function in QGIS works only on numbers, otherwise that would have been an ideal solution.spatialthoughts– spatialthoughts2019年09月25日 09:16:39 +00:00Commented Sep 25, 2019 at 9:16
-
@spatialthoughts I agree with you.
array_distinct()
would be excellent if it works with text also.ahmadhanb– ahmadhanb2019年09月25日 11:21:18 +00:00Commented Sep 25, 2019 at 11:21
Because you used regexp_matches()
instead of regexp_match()
it will return an array (notice the square brackets in Output preview at the bottom of your field calc screenshot) as opposed to a string. You need to convert the array to a string by wrapping your expression in array_to_string()
.
You also need to escape any backslashes so 1円
needs to be \1円
.
It appears that there some further tweaks you will need to make to your regex; different dialects behave differently - I would recommend testing against the Python flavour.
Explore related questions
See similar questions with these tags.
'
), not double quotes ("
) which is interpreted as a column reference (hence the error)