Wednesday, 2 May 2018

regex - Extracting text before forward slash (/) in QGIS?


I have a database of motorway junctions which are named like this:



  • M90 J8/A91/B996


  • M90 J1/A90/A921/A985(T)

  • M9 J1A


There is significant variability in the length of the strings but they all follow the same basic pattern of Motorway ID Junction Number / Link Road IDs


How do I extract everything before the first forward slash i.e. the Motorway ID and Junction Number?


I've been trying to use regex_substr based on other answers on here like this: regexp_substr( "NAME", '^(.+)/') but this extracts everything before the last forwardslash. How can I alter it to get the text before the first slash?



Answer



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.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...