Sunday 27 October 2019

qgis - Regular expression substring for labelling


I downloaded OSM .pbf raw data for Germany. Right now I'm trying to create label badges for german highways. German highways have names starting with the letter 'A' followed by a number with up to three digits.


In my dataset the information about the highway name is stored in a column called "other_tags" alongside with other information looking like this:


"bdouble"=>"yes","maxspeed"=>"none","oneway"=>"yes","ref"=>"A 7","surface"=>"asphalt"

I want to extract the 'A 7' diregarding the rest of the string. I thought about using regular expressions to do so (QGIS actually has this feature -> GREAT!), however I'm stuck implementing it without failing.


This is what I've tried using a custom expression for 'Label with':


regexp_substr( other_tags ,'A\s[0-9]+')  


My understanding of the regexp implementation is:


regexp_substr('string to analyze', 'regular expression output')


However, since that does not work I could need some help on building the expression.



Answer



Try with regexp_substr( "other_tags" ,'(A\\s[0-9]{1,3})') (tested on QGIS 2.14).


The key part are the parentheses (), corresponding to the capturing group.


For some reason, \s does not seem to work, so I replaced it with a simple blank space. As per ndawson's comment, \s simply needs to be escaped (\\s).


I also replaced [0-9]+ with [0-9]{1,3} to fit your criterion ("names starting with the letter 'A' followed by a number with up to three digits").


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...