从URL中提取MySQL或蟒蛇

问题描述:

图形号码我有了一个字符串或者具有从URL中提取MySQL或蟒蛇

hotel+4 digit number: hotel1234 
or slash+4digit.html: /1234.html 

一堆网址是否有一个正则表达式来提取1234无论是使用Python或MySQL 4位数字?

我在想 '酒店'[0-9] [0-9] [0-9] [0-9],某事像这样

谢谢!

你可以尝试REGEXP

SELECT * FROM Table WHERE ColumnName REGEXP '^[0-9]{4}$' 

SELECT * FROM Table WHERE ColumnName REGEXP '^[[:digit:]]{4}$'; 
+0

,你会知道如何正则表达式 '/1234.html'?这个确切的模式? – Sia

下面是一个stackoverflow.com链接,可能是有用的显示 how to extract a substring from inside a string in Python?

不幸的是,MySQL的正则表达式只是返回true如果字符串存在。我发现SUBSTRING_INDEX有用的,如果你知道周围目标的文字...

select case when ColumnName like 'hotel____' then substring_index(ColumnName,'hotel',-1) 
      when ColumnName like '/____.html' then substring_index(substring_index(ColumnName,'/',-1),'.html',1) 
      else ColumnName 
      end digit_extraction 
    from TableName 
where ...; 

上面的case语句是没有必要的,因为这样SUBSTRING_INDEX作品(通过返回整个字符串如果搜索字符串不找到)。

select substring_index(substring_index(substring_index(ColumnName,'hotel',-1),'/',-1),'.html',1) 
    from TableName 
where ...;