Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL substring non greedy regex

I have data like

http://www.linz.at/politik_verwaltung/32386.asp

stored in a text column. I thought a non-greedy extraction with

select substring(turl from '\..*?$') as ext from tdata

would give me .asp but instead it still ?greedely results in

 .linz.at/politik_verwaltung/32386.asp

How can I only match against the last occurence of dot .?
Using Postgresql 9.3

like image 705
JohnDoe Avatar asked Feb 11 '23 21:02

JohnDoe


1 Answers

\.[^.]*$ matches . followed by any number of non-dot characters followed by end-of-string:

# select substring('http://www.linz.at/politik_verwaltung/32386.asp' 
  from '\.[^.]*$');
 substring 
-----------
 .asp
(1 row)

As for why the non-greedy quantifiers do not work here is that they still start matching as soon as possible while still trying to match as short as possible from there on.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!