I want to create a new field in my attribute table. My goal: returning all digits between the / and _ characters of the field named "location" by applying the next expression:

regexp_substr( "Text", '/(\\d*)\_' )

based on this working example: Obtaining specific part of string from field in QGIS attribute table?

However, the expression does not give the desired result in my case:

enter image description here

Does anyone know any way to modify/accommodate the expression?

  • Does it work when you replace the * with a +? I don't really get the syntax either. Alternatively you could run a substr, since your path and length of expression is always the same.
    – Erik
    Sep 5, 2018 at 7:54

3 Answers 3


Since the number of characters is the same, you can use the substr() function on a new field as in the following expression:

substr( "Location" ,17,6)

enter image description here

In the above example, I used Path instead of Location

  • Thx a lot for answering with this example! it works perfectly :) Thxthxthx
    – abrobia
    Sep 5, 2018 at 8:19
  • 2
    Please accept the answer if it helped you solve your problem.
    – ahmadhanb
    Sep 5, 2018 at 8:46

A couple of issues - first, you don't need to escape (i.e. put a backslash before) the underscore. Your pattern also suggests that the digits follow immediately after a forward slash - which they do not, there is a w between them in each of your examples. If this is consistently a w, you could do:

regexp_substr( "location", '/w(\\d*)_' )

but in reality, if you're just trying to get every number before the underscore, you'd be sufficient with:

regexp_substr( "location", '(\\d*)_' )

As can be seen here:

enter image description here

  • How can i get all the charater after underscore. I tried regexp_substr( "location", '_(\\d*)' ) but didnt work
    – Duffer
    May 15, 2019 at 1:26
  • Looks like I found one using regexp_substr( "location", '(\\d)_' ) Thanks
    – Duffer
    May 15, 2019 at 1:31
  • 1
    Thank you, that's awesome. You can also specify the length of the digits with {5} instead of *
    – Leo
    Nov 27, 2019 at 2:18



that will return "W48535"

  • Welcome to GIS.SE. Please add some information, why this solution is preferrable over the previously provided ones.
    – Erik
    Aug 13, 2020 at 8:38

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.