简体   繁体   English

使用SQL进行模式匹配

[英]Pattern Matching Using SQL

I need to match bellow mentioned patterns using SQL. 我需要使用SQL来匹配下面提到的模式。

  1. "* THANK YOU" “* 谢谢”
  2. "* MUSIC *" “ *音乐*”
  3. "I LIKE * MUSIC" “我喜欢音乐”
  4. "I LIKE * MUSIC AND * PAINTING" “我喜欢*音乐和*绘画”

Input strings will look like this, 输入字符串看起来像这样,
"HI THANK YOU" -> should match "* THANK YOU" “嗨,谢谢”->应该与“ *谢谢”匹配
"I LIKE MUSIC VERY MUCH" -> should match "* MUSIC *" “我非常喜欢音乐”->应匹配“ *音乐*”

Is there a way to do this using standard SQL syntax. 有没有办法使用标准SQL语法来做到这一点。

Please help. 请帮忙。

Thank you. 谢谢。

Oracle MySQL Sample Queries: Oracle MySQL示例查询:

Case insensitive: 不区分大小写:

select * from database where column like '%THANK YOU%';

Case sensitive: 区分大小写:

select * from database where column LIKE BINARY '%THANK YOU%';

Case insensitive: 不区分大小写:

select * from database where column REGEXP 'THANK YOU';

Oracle Queries: Oracle查询:

select * from database WHERE REGEXP_LIKE(column, 'THANK YOU', 'i')

You can use LIKE in Oracle, but case insensitivity setup depends on the version. 您可以在Oracle中使用LIKE,但是不区分大小写的设置取决于版本。

Case sensitive: 区分大小写:

select * from database WHERE column LIKE 'THANK YOU';

There is a lot of discussion about making Oracle's like case insensitive. 关于使Oracle的大小写不敏感,有很多讨论。 It's easier to use the reg_exp version for that and set the i flag for case insensitivity. 为此,使用reg_exp版本更容易,并设置i标志以区分大小写。

Update The original question was actually opposite of my first answer, and it has really opened up my head about quite a few apps that I'm building for datamining. 更新最初的问题实际上与我的第一个答案相反,并且确实使我对我为数据挖掘而构建的许多应用程序打开了头脑。 Actually had this idea last night but the original poster posted it before I did (Fiddle was down). 昨晚实际上有这个想法,但是原始海报在我之前就发布了(Fiddle失败了)。 You can make Regex match both sides of the REGEXP function in MySQL. 您可以使Regex与MySQL中REGEXP函数的两侧匹配。 So if you store a list of expressions into the database, then you can actually compare the list of expressions against a table of entries or a single string. 因此,如果将表达式列表存储到数据库中,则实际上可以将表达式列表与条目表或单个字符串进行比较。

I've added an SQL Fiddle here: http://sqlfiddle.com/#!8/701a0/2 我在这里添加了SQL Fiddle: http ://sqlfiddle.com/#!8 / 701a0 / 2

Note that in the original example, the "regex" values in the database started with just an asterisk. 请注意 ,在原始示例中,数据库中的“ regex”值仅以星号开头。 In Regex, the actual string would need to be .* for possibly any character. 在Regex中,对于任何字符,实际字符串都必须为。*。

I've added a few examples for matching things like websites, US Toll Free Numbers, and gmail accounts. 我添加了一些示例来匹配网站,美国免费电话和gmail帐户。

INSERT INTO `myRegex` VALUES (1,'.* THANK YOU$'),(2,'MUSIC'),(3,'I LIKE .* MUSIC'),(4,'I LIKE .* MUSIC AND .* PAINTING'),(5,'^[0-9]?[ .-]?(800|866)'),(6,'\\.com$'),(7,'\\.net$'),(8,'\\.(gmail|googlemail)\\.'),(9,'THANK YOU');

Output 产量

select * from myRegex where 'HI THANK YOU' REGEXP reg_expr;

在此处输入图片说明

select * from myRegex where 'THANK YOU MUCH' REGEXP reg_expr;

在此处输入图片说明

I found a way to do this using MySQL, 我找到了一种使用MySQL的方法,

SELECT * FROM knowledge WHERE 'HI THANK YOU' REGEXP pattern_col; 选择*从知识中“嗨,谢谢” REGEXP pattern_col;

pattern_col holds regexps. pattern_col保存正则表达式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM