I hope to use
QgsFeatureRequest(QgsExpression) to get desired features.
The statement in QgsExpression is:
expr = QgsFeatureRequest(QgsExpression("\"planid\" in (48757) AND spi like '0\\%'")
The feature has
"planid" = 48757, and
"spi" field is string like
So I intended to match
'0\\' in the
QgsExpression, but it didn't work at all. I got nothing returned.
If I changed the string to
"\"planid\" in (48757) AND spi like '0%'" by removing
'\\', then it could return a feature.
But I really need
'\\', since it's a very important character.
So how should I modify the string to make it work?
From @MatthiasKuhn suggestion, I tested to use '\\\\' in QgsExpression, like
"\"planid\" in (48757) AND spi like '0\\\\%'".
It didn't work from Python scripts, but I did get a hint from it. After running the statement from "Select by Expression" in QGIS, four '\' worked fine. So this is the correct format.
Therefore, I built the statement in Python with 8 '\', and it did work.
expr = QgsFeatureRequest(QgsExpression("\"planid\" in (48757) AND spi like '0\\\\\\\\%'")
I understand why Python needs to double the number of '\', but can @MatthiasKuhn or anyone else explain why I need four '\' here in QGIS?