I have an attribute table that contains in a field/slot called "Description" data like this:

<b>xyz: 11.02.2016 04:05 - 14.02.2016 04:03</b><br/><br/>0 km/h, 0 hauk/min , ODO 0m 

<b>xyz: 01.03.2016 04:01</b><br/><br/>0 km/h, 0 hauk/min , ODO 0m 

0 km/h, 0 hauk/min , ODO 0m 

In two of these three samples is a date and a time. Actually the first one has a time period.

I want to automatically extract the substrings that contain this data and if this data is not included like in the last sample then nothing should be extracted.

I was thinking that everything between > (from <b>) and < (from </b>) could be extracted.

How can I achieve that?

Please note that I have no coding experience with Qgis Python. I recently started to learn Python 3 online, but in Qgis Python is different. I use QGIS 2.12.

2 Answers 2


If you want to extract the content of the first <b> tags of each field, you can use the following expression in the field calculator:

 regexp_substr( "fieldname", '^<b>(.*)</b>')

With your samples, this results in:

xyz: 11.02.2016 04:05 - 14.02.2016 04:03
xyz: 01.03.2016 04:01

If you need something more advanced (such as extracting the timestamps only), please edit your question to add more details.

  • Thanks for all answers and sorry for my late feedback on this. The answer from ArMoraer did the trick. Thanks. Jun 10, 2016 at 11:16

If your data is regular enough that your data is always between b and /b it should be possible without using special python expressions. The Idea is to use the "substr" function for this. But as it is not always the same length and position probably you need to find the position first. That can be done with the function "strpos" that gives you the position of your search phrase.

substr("fieldname",strpos("fieldname", '<b>')+3,strpos("fieldname", '</b>'))

The +3 is because the strpos delivers the position at the beginning of the string and you don´t need to have the brackets in your calculated string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.