4

I try to extract the filename from a path in my attribute table in QGIS.

regex_substr should be the way to go I assume, but testing all kinds of example from the web did not work out.

I have images which I want to link, but the original pathes are not valid anymore, cause all images from different people went into one single folder now.

Example paths:

./20170608-attachments/p__20170608_122128.jpg
./20170603-attachments/p__20170603_095124.jpg

here I would just like to populate a new field with p__20170608_122128.jpg etc. only.

A solution, that would give me the rest of the string after the last / would be the most versatile one in my case, I assume.

Any ideas?

3
  • YouTube may try your_string[your_string.rfind('/')+1:] ír
    – Zoltan
    Commented Oct 22, 2017 at 16:27
  • Try rfind('/') instead öv regexp function
    – Zoltan
    Commented Oct 22, 2017 at 16:29
  • There is no such thing as rfind in the field calculator expressions for strings, otherwise I would have used it.
    – Bernd V.
    Commented Oct 22, 2017 at 16:40

1 Answer 1

5

Found a working solution for Linux myself shortly after posting the question.

([^/]+$)

gives the rest of the string (in my case the filename) after the last /

regexp_substr("column_with_full_path", '([^/]+$)')

Source: https://stackoverflow.com/q/8945477

For Windows, referring to cm1's comment below:

regexp_substr("column_with_full_path", '([^\\\\]+$)')
1
  • In my case, I had backslashes (Windows) in the field named 'location'. This worked nicely: regexp_substr("location", '([^\\\\]+$)'). Use double backslashes to represent a single backslash (one 'escapes' the other).
    – cm1
    Commented Mar 26, 2018 at 13:45

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.