Deleting any characters after certain character in QGIS label expression

You can use regular expressions like this:

 regexp_substr("yourtextfield",'[^;]*')

To match more characters than ; you can just add it to this expression like:

regexp_substr("yourtextfield",'[^;,]*')

which will return all values before ; or , characters.

enter image description here


You can use a comibination of the expressions strpos and left. Left returns the characters of your text from the left up to the value defined. With strpos you can get the position of a certain character in a string ("finding the position of the comma in your text"). Set this -1 and you have the position until which you want your original string to go.

Combining it both for comma and semicolon, the expression looks like this, where "val" is the name of your attribute field:

left (
    left ( 
        "val", 
        ( strpos ( "val", ',') -1 )
    ), 
    ( strpos( "val", ';') -1 )
)

Just for completion, you can also use arrays in QGIS expressions in combination with array_foreach to replace a series of delimeters, without using regular expressions. The following expression removes everything after the delimiters . , ; - _ However, in this case using the expression used by @MrXsquared is more elegant than this more complex expression:

array_to_string (
    array_remove_all ( 
        ( array_foreach 
            ( array ('.',',',';','-','_'), 
            if (   
                strpos (  "text", @element) =0 
                , 'delete',
                left ( 
                    "text", 
                    ( strpos ( "text", @element) - 1)
                )
            ))
        ), 
    'x')
)

enter image description here