This is based on Run regexp in PyQGIS script? and Search and Replace text in All Fields in QGIS 3
I am trying to modify the code to allow more complex replacements using regex but am unsure how to structure the re.sub function
See https://regex101.com/r/OX1W3b/2/ for one example...
1. DIMENSIONS: | ORIGIN: | Position corrected and IL (0) was changed based on RPS: 3482 -230
2. DIMENSIONS: 2 x 1350 RCP | ORIGIN: PCD13180 | Position corrected and IL (0) was changed based on RPS: 1390 -20800/1350RCP
3. DIMENSIONS: 3 x 375 RCP | ORIGIN:
4. DIMENSIONS: | ORIGIN:
5. Review attribution | DIMENSIONS: | ORIGIN:
Where there is no value after DIMENSIONS or ORIGIN it should be deleted so...
1. Position corrected and IL (0) was changed based on RPS: 3482 -230
2. DIMENSIONS: 2 x 1350 RCP | ORIGIN: PCD13180 | Position corrected and IL (0) was changed based on RPS: 1390 -20800/1350RCP
3. DIMENSIONS: 3 x 375 RCP
4.
5. Review attribution
Here's the current code - see line 10 and 33 for the regex based commands.
#Use in QGIS 3 python console
#Search and replace text in all fields of all layers
#Also allows regex use
import re
#Based on https://gis.stackexchange.com/questions/317855/search-and-replace-text-in-all-fields-in-qgis-3
#set text to search for and replace with.
searchText = "DIM"
regexText = ".*(DIMENSIONS: \|)"
replaceText = ""
#run on all layers
layers = QgsProject.instance().mapLayers()
for layer_id, layer in layers.items():
i=1
print("Layer: %s" % (layer.name()))
# get data provider
dpr = layer.dataProvider()
for field in layer.fields():
fieldName=field.name()
for feature in layer.getFeatures():
inText = str(feature[fieldName])
# get field index
fieldIndex = layer.fields().indexFromName(fieldName)
#print ("Checking %s" % (inText))
if searchText in inText:
# change inText
print ("%s . REPLACED: %s in %s with %s in column: %s" % (i, searchText, inText, replaceText, fieldName))
#outText = inText.replace(searchText, replaceText)
outText = re.sub(fieldName, regexText, replaceText)
i+=1
# save changes
dpr.changeAttributeValues({feature.id(): {fieldIndex: outText}})
print ("Completed")
UPDATE when using regexText = ".*(DIMENSIONS: )(\| ORIGIN: ?\|? ?)"
I think we need to deal with each key (DIMENSIONS:, ORIGIN: etc) without value separately as there are others and it overly complicates the regex. So forget about ORIGIN: and need to look at code that will just clean up DIMENSIONS as per the examples. The same can then be used for ORIGIN once we get it right.
Basically we just need to get rid of the key (DIMENSIONS:) when it doesn't have text after it and before the | seperator. Any text before or after should be retained as per the examples.
I think the issue is in the use of "outText = re.sub(fieldName, regexText, replaceText)" because the value in replaceText should be the capture string from the regex not the string in replaceText.