14

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?

1
  • 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. Commented Sep 5, 2018 at 7:54

3 Answers 3

21

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

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

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

3
  • How can i get all the charater after underscore. I tried regexp_substr( "location", '_(\\d*)' ) but didnt work Commented May 15, 2019 at 1:26
  • Looks like I found one using regexp_substr( "location", '(\\d)_' ) Thanks Commented 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 * Commented Nov 27, 2019 at 2:18
1
substr("location",16,(length("location")-1))

or

substr(substr("location",17,14),1,6)

that will return "W48535"

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.