3

I am trying to remove the last piece of my string in QGIS.

enter image description here

I want to delete the E10 from the end.

I tried both:

How to easily edit attribute data using Regular Expressions?

 regexp_replace("UPRN", 'E*', '')

as well as

 regexp_replace("UPRN", 'E10', '')

but I get only pure 1.000 value.

In this approach:

Removing string to the right of a character using QGIS Field Calculator?

I tried:

 regexp_replace("UPRN", ('E*'), '')

and

 regexp_replace("UPRN", ('E*'), '')

but it still throws 1.0000 only.

What am I missing here?

3
  • It should be E.*, surely?
    – Encomium
    Nov 24, 2021 at 17:34
  • What is the precision of your UPRN4 field, do all the decimals fit or are they truncated to 1.000?
    – BERA
    Nov 24, 2021 at 18:42
  • The precision is as you can see on the left. About 8 decimals...
    – Geographos
    Nov 24, 2021 at 21:27

1 Answer 1

6

You're missing the . after the E; in other words, the token for a character following the E:

regexp_replace(string, 'E.*', '')

enter image description here

There are a lot of ways to make the pattern more surgical. You could use \d if you want to match an E and any digits that follow it:

regexp_replace(string, 'E\d*', '')

You can also use + rather than * to ensure you only match to literal E that are actually followed by at least one digit:

regexp_replace(string, 'E\d+', '')

Just depends on how much variation there is in your strings.

P.S. It also wouldn't surprise me if you're saving the results in a float, rather than a string field. Might explain the case where you get 1.000 as your result

3
  • I saved these results as a float exactly. Your code is working. Thanks. How about the single dot removal from the string?
    – Geographos
    Nov 25, 2021 at 9:59
  • 1
    As '.' is a special character in regex, you need to use a backslash to escape it. e.g. regex_replace(string,'\.','')
    – Encomium
    Nov 25, 2021 at 14:49
  • I've already sorted it in a different way. Many thanks for your help :)
    – Geographos
    Nov 25, 2021 at 16:32

Your Answer

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

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