I want to replace a '+' with ', ' to label an attribute looking like "ZT10+ZT20+ZT30" to get "ZT10, ZT20, ZT30" using

regexp_replace("zone_nr", '+', ', ')

but I get an Eval Error: Invalid regular expression '+': quantifier does not follow a repeatable item.

  • 1
    Have you tried replace("zone_nr", '+', ', ')? – Taras Oct 22 at 9:15
  • is using regexp_replace() mandatory? – Jochen Schwarze Oct 22 at 11:43
up vote 5 down vote accepted

A + is a special symbol to the QGis regular expression engine (it means 1 or more of the preceding character), so you need to escape it by putting a \ in front of it. So you need:

regexp_replace("zone_nr", '\\+', ', ')
  • 1
    Backslash characters must be double escaped ` \\ ` – Oto Kaláb Oct 22 at 9:26
  • but only if I want a \ not if using it as an escape – Ian Turton Oct 22 at 9:34
  • 1
    @IanTurton actually, no, you need to double escape all backslashes, e.g. '\\\\' would match one backslash. this covers more. or did this change in QGIS 3.x? – ThingumaBob Oct 22 at 9:52
  • You are right, I'm in java mode just now! – Ian Turton Oct 22 at 9:57

Or, simply use

replace("zone_nr", '+', ', ')

as this doesn't expect a regexp term (it simply matches all occurences of the specified input string).

You can input regular expression to find every single + like [+]

regexp_replace("zone_nr", '[+]', ', ')
  • 2
    btw., this will replace the whole string with ', ' if a '+' was found in it! – ThingumaBob Oct 22 at 10:37
  • You're right I improved my answer – Oto Kaláb Oct 22 at 13:59

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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