This is based on https://gis.stackexchange.com/questions/254249/run-regexp-in-pyqgis-script and https://gis.stackexchange.com/questions/317855/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: ?\|? ?)"`
[![enter image description here][1]][1]

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.

  [1]: https://i.sstatic.net/HI9pQ.png