0

I'm trying to use regex expression to label my layer in order to show the parcel number. Here is some examples of what i'm looking for :

From this "53042A0027/00M002", to "A27M/2"
From this "53042A2000/02A003", to "A2000A2/3"
From this "53042C0002/00_000", to "A2"
From this "53042G1257/00X000", to "G1257X"

EDIT (rules to consider) :

Copy the location 6 ==> "53042**G**1257/00X000" 
Then for location 7 to 10, add numbers after the first not null number ==> "53042A**2000**/02A003", "53042A00**27**/00M002"
Then copy location 14 if not equal to "_" ==> "53042A0027/00**M**002"
Then for location 12 and 13, addnumbers after the first not null number ==> "53042A2000/0**2**A003", "53042A0027/00M002"
Then for location 15 to 17, add "/" and all numbers after the first not null number ==> "53042A2000/02A00**3**", "53042A0027/00M00**2**"
9

1 Answer 1

2

This function work like expected (at least for the 4 examples) :

regexp_replace(
    regexp_replace(
        "Test", 
        '[0-9]{5}([A-Z])0*([1-9][0-9]*)/(0|([1-9]))*(_|([A-Z]))(0|([1-9])){3}', 
        '\\1\\2\\6\\4/\\8'),
     '/$', 
     ''
)

The second 'regexp_replace' function (the one with the big regexp) defines eight groups (the values in brackets) that you can use to generate a new string, like explain by the next figure.

Rexep_replace example

The first 'regexp_replace' remove the slash / character when the new string when it is the last character (I can't do it in the same function).

2
  • It works perfectly fine ! Thank you :) If i want to add the symbol "^" before the group \\4 when the number is 1 to 9 how should i proceed ? Commented Apr 8, 2022 at 13:37
  • This is usually possible with the conditional statement syntax, like here : regex101.com/r/Og9T8H/1 But this syntax does not work in qgis. I don't know if there is an alternative syntax in qgis.
    – Atm
    Commented Apr 8, 2022 at 15:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.