Skip to main content
Tweeted twitter.com/StackGIS/status/1117850427427446785
added some background info
Source Link
GeorgeC
  • 8.3k
  • 7
  • 56
  • 143

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 OriginORIGIN: 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.

I think we need to deal with each key 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.

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.

Became Hot Network Question
added screen grab and example as per comments
Source Link
GeorgeC
  • 8.3k
  • 7
  • 56
  • 143

UPDATE when using regexText = ".*(DIMENSIONS: )(\| ORIGIN: ?\|? ?)" enter image description here

I think we need to deal with each key 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.

UPDATE when using regexText = ".*(DIMENSIONS: )(\| ORIGIN: ?\|? ?)" enter image description here

I think we need to deal with each key 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.

added example and cleaned code
Source Link
GeorgeC
  • 8.3k
  • 7
  • 56
  • 143

See https://regex101.com/r/OX1W3b/1/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:
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 3733 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 only on active layer
#layer = iface.activeLayer()

#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")

See https://regex101.com/r/OX1W3b/1/ 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:
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. 

Here's the current code - see line 10 and 37 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 only on active layer
#layer = iface.activeLayer()

#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")

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:
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")
Source Link
GeorgeC
  • 8.3k
  • 7
  • 56
  • 143
Loading