5

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.

  • Would you like to remove the / as well? – SaultDon May 17 '17 at 23:22
10

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

  • Thank you! This worked great and your explanation is very thorough! – Ryan Jones May 18 '17 at 14:23

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.