3

I have a shapefile of data for Japan, and I need to extract the English character names in my "name" field in order to label the layer. Much of the data is all in Japanese, but some of the names have an alternate English name which can be found inside a set of parenthesis. I'd like to extract this text to another field and use it for labeling. I'm using QGIS.

Example:

I need to turn

野川サイクリング道路 (Nogawa Cycling Road)  

Into

Nogawa Cycling Road

I'm attempting to use the regexp_substr function in the Field Calculator:

regexp_substr( "name", '\((.*?)\)')

When I try to run this, I get an error:

An error occured while evaluating the calculation string: Invalid regular expression '?(.*?)?': bad repetition syntax.

Oddly, in testing, I tried this same regex in PostGIS and it worked great. Does anyone know of a regex that will do this task in the QGIS field calculator?

5

I tried this, and it seems to work..

regexp_substr( "name", '[(](.*)?[)]')

You need to use greedy matching (hence the ?), but also need to put square brackets around the round brackets.

  • 1
    You can also escape the parens with \\( and \\) - I thought it might only need one backslash, and so be shorter than [)] but you need two (there's some deeper string interpolation going on....) – Spacedman Oct 12 '15 at 21:56
  • +1, don't think I've seen that many brackets in a single statement! – Joseph Oct 13 '15 at 10:32
  • Thank you all! regexp_substr( "name", '[(](.*)?[)]') did the trick! – Christy Heaton Oct 14 '15 at 15:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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