I'm attempting to remove a part of string in an attribute column of a shapefile. The part in question is Snapshots\\ from Snapshots\390_measure_1.jpeg. So I want the output to be 390_measure_1.jpeg.

Right now I'm using the Field Calculator like so to do this:

regexp_replace("Snapshot_1", '([a-zA-Z])+\\ ([0-9])+_([a-zA-Z])+\.([a-zA-Z])+', '\\2\\3')

This however does nothing to the output. I don't understand what I'm doing wrong. Also there seems to be no way to escape \\ (The backslash character) in order to match it separately.

  • Sorry, what do you mean by escape "" (The backslash character) in order to match it seperately? backslash is part of the string? You want to keep backslashes or not?
    – Babel
    Dec 27, 2022 at 13:42
  • I wanted to remove the backslash, so I needed to escape it in regex in order for regex to match it. If I didn't escape it, regex wouldn't realise that it's a part of the string, rather than a part of regex syntax. Sorry for confusing terminology, I'm not a programmer or a native speaker of English.
    – enor
    Dec 28, 2022 at 11:24
  • 1
    Yes, as you found out, two backslashes \\ are used in QGIS expression engine regex to mask characters
    – Babel
    Dec 28, 2022 at 11:26
  • This question is clearly addressed in the regexp_replace - QGIS Documentation: "Backslash characters must be double escaped." Since the normal regex for a literal backslash is '\\' and backslash characters need to be double escaped in QGIS regexp functions, you end up with '\\\\'.
    – bixb0012
    Dec 29, 2022 at 15:44

4 Answers 4


The correct way to do this seems to be


This removes "Snapshots\" from the regex as it somehow escapes the backslash.


You could just split on the \ character and return the last element of the array:

split( "Snapshots_1", '\\', -1)

enter image description here

The custom split function can be defined in the Function Editor:

enter image description here

from qgis.core import *

@qgsfunction(args='auto', group='Custom')
def split(value, char, index, feature, parent):
    Splits the parameter on char and returns split[index].
    <h2>Example usage:</h2>
      <li>split("a_b", "_", 0) -> a</li>
      <li>split("a b c", " ", 1) -> b</li>
    return value.split(char)[index]
  • Sorry, this does not provide an answer as split function is a custom function, not predifined (maybe you defined it earlier, so it already existist). You should provide the code to create the split function, otherwise this solution does not work.
    – Babel
    Dec 27, 2022 at 13:50
  • 1
    @Babel, I've never defined it, but have included the code.
    – user2856
    Dec 27, 2022 at 23:03

Two other options how to do it: one without, the other with Regular Expressions.

  1. Without using regular expressions:

    replace (
        left ("Snapshot_1",10),

    This looks for the 10 characters at the left (first 10 characters, line 3) of the input string (line 2) and replaces them with an empty string (line 4).

    If \ (backslash character) is part of the input string, replace 10 with 11 in the expression.

  1. With regular expressions


    This looks for the position of the first backslash \ and returns the substring based on the input starting with the next character: from the first character after the \ till the end.


Another option is to use substr() and strpos(). strpos(string, char) returns the position of the first match for char in the string. substr(string, start[, optional length]) returns the part of the string starting at position start and an optional length, which isn't needed here.

Position of the first backslash: strpos(Snapshot_1, '\\')

Part of the string starting after the backslash. (Note: we need to add 1 position to set the start after the backslash.) substr(Snapshot_1, strpos(Snapshot_1, '\\')+1)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.