13

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?

Taras
  • 32,823
  • 4
  • 66
  • 137
abrobia
  • 137
  • 1
  • 1
  • 8
  • 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 05 '18 at 07:54

3 Answers3

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

ahmadhanb
  • 40,826
  • 5
  • 51
  • 105
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

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

or

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

that will return "W48535"

Taras
  • 32,823
  • 4
  • 66
  • 137
user168488
  • 11
  • 2
  • Welcome to GIS.SE. Please add some information, why this solution is preferrable over the previously provided ones. – Erik Aug 13 '20 at 08:38