Extracting text before forward slash (/) in QGIS?

QGIS' regex engine doesn't support non greedy matches (yet), so it'll always return the longest possible match. Try

regexp_substr( "NAME", '^[^/]+')

That should return all characters from the start of the string which aren't /s.


This turns out to be doable without using the regex_substr function. Instead the code

left( "Name", strpos( "Name" ,'/'))`

can be used instead. It works by identifying the string position of the first / and then extracting the text to the left of that position.

If anyone has any suggestions regarding regex_substr I'd still like to see them though.


Not sure where in QGIS you want this feature work. If In the field calculator you may define a custom function based on the Python function split().

This image shows the basic definition of such a function. split('/')[0] always returns the text before the first occurrence of '/' or the complete string, if no '/' is found.

split function definition

You can use this new function like this:

usage of split function

Tags:

Regex

Qgis