Matching Backslash with Regex in QGIS Field Calculator – How To Guide

field-calculatorfields-attributesqgisregular expressionreplace

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.

Best Answer

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.