QGIS Field Calculator – Removing String to the Right of a Character

field-calculatorqgis

I have a string field and I want to delete the part of the string after a specific character in this case a '/'. I am looking for a way to do this using the field calculator in QGIS.

I know I can use the replace function to replace a specific string but I need a way to do this with a wildcard as the strings after the '/' character are not the same in the table.

Best Answer

You can use a regular expression with the field calculator function regexp_replace() on a NEW field like:
regexp_replace("input_field",'(/.*$)','/')

That one will keep the / but if you want the / removed, use: regexp_replace("test_regex",'(/.*$)','')

Explanation: The regular expression being used is /.*$

/ --look for a forward slash

. --identify any character (strings/numbers/special) after the slash except newlines

* --keep searching for any characters after the slash

$ --until the end of the string is reached