本文介绍了从 url mysql 或 python 中提取模式号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆 url 有一个字符串或者有

I have a bunch url that has a string either has

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

是否有使用 python 或 mysql 提取 1234 这样的 4 位数字的正则表达式?

Is there a regex to extract 4 digit number like 1234 either use python or mysql?

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

I'm thinking 'hotel'[0-9][0-9][0-9][0-9],sth like this

谢谢!

推荐答案

以下是 stackoverflow.com 链接,可能对展示有用如何从字符串中提取子字符串蟒蛇?

The following is a stackoverflow.com link that might be useful showinghow to extract a substring from inside a string in Python?

不幸的是,如果字符串存在,MySQL regexp 只会返回 true.如果您知道目标周围的文本,我发现 substring_index 很有用...

Unfortunately, MySQL regexp simply returns true if the string exists. I have found substring_index useful if you know the text surrounding the target...

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

由于 substring_index 的工作方式(如果未找到搜索字符串则返回整个字符串),因此不需要上述 case 语句.

The case statement above isn't necessary because of the way substring_index works (by returning the entire string if the search string isn't found).

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

这篇关于从 url mysql 或 python 中提取模式号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 12:48
查看更多