2

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.

4
  • 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
    Commented 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
    Commented 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
    Commented Dec 28, 2022 at 11:26
  • This question is clearly addressed in the 12.2.24.12. 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
    Commented Dec 29, 2022 at 15:44

4 Answers 4

5

The correct way to do this seems to be

regexp_replace("Snapshot_1",'Snapshots\\\\','')

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

0
4

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>
    <ul>
      <li>split("a_b", "_", 0) -> a</li>
      <li>split("a b c", " ", 1) -> b</li>
    </ul>
    """
    return value.split(char)[index]
2
  • 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
    Commented Dec 27, 2022 at 13:50
  • 1
    @Babel, I've never defined it, but have included the code.
    – user2856
    Commented Dec 27, 2022 at 23:03
3

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

  1. Without using regular expressions:

    replace (
        "Snapshot_1",
        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

    substr( 
        "Snapshot_1",
        regexp_match( 
            "Snapshot_1",
            '\\\\'
        )+1
    )
    

    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.

2

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 you have read our privacy policy.

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