sql 实现正则表达式

作者&投稿:素顾 (若有异议请与网页底部的电邮联系)
怎么用正则表达式解析sql语句~

先看要解析的样例SQL语句:

select * from dual
SELECT * frOm dual
Select C1,c2 From tb
select c1,c2 from tb
select count(*) from t1
select c1,c2,c3 from t1 where condi1=1
Select c1,c2,c3 From t1 Where condi1=1
select c1,c2,c3 from t1,t2 where condi3=3 or condi4=5 order by o1,o2
Select c1,c2,c3 from t1,t2 Where condi3=3 or condi4=5 Order by o1,o2
select c1,c2,c3 from t1,t2,t3 where condi1=5 and condi6=6 or condi7=7 group by g1,g2
Select c1,c2,c3 From t1,t2,t3 Where condi1=5 and condi6=6 or condi7=7 Group by g1,g2
Select c1,c2,c3 From t1,t2,t3 Where condi1=5 and condi6=6 or condi7=7 Group by g1,g2,g3 order by g2,g3

解析效果之一(isSingleLine=false):

原SQL为select * from dual
解析后的SQL为
select
*
from
dual

原SQL为SELECT * frOm dual
解析后的SQL为
select
*
from
dual

原SQL为Select C1,c2 From tb
解析后的SQL为
select
C1,c2
from
tb

原SQL为select c1,c2 from tb
解析后的SQL为
select
c1,c2
from
tb

原SQL为select count(*) from t1
解析后的SQL为
select
count(*)
from
t1

原SQL为select c1,c2,c3 from t1 where condi1=1
解析后的SQL为
select
c1,c2,c3
from
t1
where
condi1=1

原SQL为Select c1,c2,c3 From t1 Where condi1=1
解析后的SQL为
select
c1,c2,c3
from
t1
where
condi1=1

原SQL为select c1,c2,c3 from t1,t2 where condi3=3 or condi4=5 order by o1,o2
解析后的SQL为
select
c1,c2,c3
from
t1,t2
where
condi3=3 or condi4=5
order by
o1,o2

原SQL为Select c1,c2,c3 from t1,t2 Where condi3=3 or condi4=5 Order by o1,o2
解析后的SQL为
select
c1,c2,c3
from
t1,t2
where
condi3=3 or condi4=5
order by
o1,o2

原SQL为select c1,c2,c3 from t1,t2,t3 where condi1=5 and condi6=6 or condi7=7 group by g1,g2
解析后的SQL为
select
c1,c2,c3
from
t1,t2,t3
where
condi1=5 and condi6=6 or condi7=7
group by
g1,g2

原SQL为Select c1,c2,c3 From t1,t2,t3 Where condi1=5 and condi6=6 or condi7=7 Group by g1,g2
解析后的SQL为
select
c1,c2,c3
from
t1,t2,t3
where
condi1=5 and condi6=6 or condi7=7
group by
g1,g2

原SQL为Select c1,c2,c3 From t1,t2,t3 Where condi1=5 and condi6=6 or condi7=7 Group by g1,g2,g3 order by g2,g3
解析后的SQL为
select
c1,c2,c3
from
t1,t2,t3
where
condi1=5 and condi6=6 or condi7=7
group by
g1,g2,g3
order by
g2,g3

解析效果之二(isSingleLine=true):

原SQL为select * from dual
解析后的SQL为
select
*
from
dual

原SQL为SELECT * frOm dual
解析后的SQL为
select
*
from
dual

原SQL为Select C1,c2 From tb
解析后的SQL为
select
C1,
c2
from
tb

原SQL为select c1,c2 from tb
解析后的SQL为
select
c1,
c2
from
tb

原SQL为select count(*) from t1
解析后的SQL为
select
count(*)
from
t1

原SQL为select c1,c2,c3 from t1 where condi1=1
解析后的SQL为
select
c1,
c2,
c3
from
t1
where
condi1=1

原SQL为Select c1,c2,c3 From t1 Where condi1=1
解析后的SQL为
select
c1,
c2,
c3
from
t1
where
condi1=1

原SQL为select c1,c2,c3 from t1,t2 where condi3=3 or condi4=5 order by o1,o2
解析后的SQL为
select
c1,
c2,
c3
from
t1,
t2
where
condi3=3 or
condi4=5
order by
o1,
o2

原SQL为Select c1,c2,c3 from t1,t2 Where condi3=3 or condi4=5 Order by o1,o2
解析后的SQL为
select
c1,
c2,
c3
from
t1,
t2
where
condi3=3 or
condi4=5
order by
o1,
o2

原SQL为select c1,c2,c3 from t1,t2,t3 wher www.hnnedu.com e condi1=5 and condi6=6 or condi7=7 group by g1,g2
解析后的SQL为
select
c1,
c2,
c3
from
t1,
t2,
t3
where
condi1=5 and
condi6=6 or
condi7=7
group by
g1,
g2

原SQL为Select c1,c2,c3 From t1,t2,t3 Where condi1=5 and condi6=6 or condi7=7 Group by g1,g2
解析后的SQL为
select
c1,
c2,
c3
from
t1,
t2,
t3
where
condi1=5 and
condi6=6 or
condi7=7
group by
g1,
g2

原SQL为Select c1,c2,c3 From t1,t2,t3 Where condi1=5 and condi6=6 or condi7=7 Group by g1,g2,g3 order by g2,g3
解析后的SQL为
select
c1,
c2,
c3
from
t1,
t2,
t3
where
condi1=5 and
condi6=6 or
condi7=7
group by
g1,
g2,
g3
order by
g2,
g3

使用的类SqlParser,你可以拷贝下来使用之:

package com.sitinspring.common.sqlFormatter;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* SQL语句解析器类
* @author: sitinspring(junglesong@gmail.com)
* @date: 2008-3-12
*/
public class SqlParser{
/**
* 逗号
*/
private static final String Comma = ",";

/**
* 四个空格
*/
private static final String FourSpace = " ";

/**
* 是否单行显示字段,表,条件的标识量
*/
private static boolean isSingleLine=true;

/**
* 待解析的SQL语句
*/
private String sql;

/**
* SQL中选择的列
*/
private String cols;

/**
* SQL中查找的表
*/
private String tables;

/**
* 查找条件
*/
private String conditions;

/**
* Group By的字段
*/
private String groupCols;

/**
* Order by的字段
*/
private String orderCols;

/**
* 构造函数
* 功能:传入构造函数,解析成字段,表,条件等
* @param sql:传入的SQL语句
*/
public SqlParser(String sql){
this.sql=sql.trim();

先看要解析的样例SQL语句:

select * from dual
SELECT * frOm dual
Select C1,c2 From tb
select c1,c2 from tb
select count(*) from t1
select c1,c2,c3 from t1 where condi1=1
Select c1,c2,c3 From t1 Where condi1=1
select c1,c2,c3 from t1,t2 where condi3=3 or condi4=5 order by o1,o2
Select c1,c2,c3 from t1,t2 Where condi3=3 or condi4=5 Order by o1,o2
select c1,c2,c3 from t1,t2,t3 where condi1=5 and condi6=6 or condi7=7 group by g1,g2
Select c1,c2,c3 From t1,t2,t3 Where condi1=5 and condi6=6 or condi7=7 Group by g1,g2
Select c1,c2,c3 From t1,t2,t3 Where condi1=5 and condi6=6 or condi7=7 Group by g1,g2,g3 order by g2,g3

解析效果之一(isSingleLine=false):

原SQL为select * from dual
解析后的SQL为
select
*
from
dual

原SQL为SELECT * frOm dual
解析后的SQL为
select
*
from
dual

原SQL为Select C1,c2 From tb
解析后的SQL为
select
C1,c2
from
tb

原SQL为select c1,c2 from tb
解析后的SQL为
select
c1,c2
from
tb

原SQL为select count(*) from t1
解析后的SQL为
select
count(*)
from
t1

原SQL为select c1,c2,c3 from t1 where condi1=1
解析后的SQL为
select
c1,c2,c3
from
t1
where
condi1=1

原SQL为Select c1,c2,c3 From t1 Where condi1=1
解析后的SQL为
select
c1,c2,c3
from
t1
where
condi1=1

原SQL为select c1,c2,c3 from t1,t2 where condi3=3 or condi4=5 order by o1,o2
解析后的SQL为
select
c1,c2,c3
from
t1,t2
where
condi3=3 or condi4=5
order by
o1,o2

原SQL为Select c1,c2,c3 from t1,t2 Where condi3=3 or condi4=5 Order by o1,o2
解析后的SQL为
select
c1,c2,c3
from
t1,t2
where
condi3=3 or condi4=5
order by
o1,o2

原SQL为select c1,c2,c3 from t1,t2,t3 where condi1=5 and condi6=6 or condi7=7 group by g1,g2
解析后的SQL为
select
c1,c2,c3
from
t1,t2,t3
where
condi1=5 and condi6=6 or condi7=7
group by
g1,g2

原SQL为Select c1,c2,c3 From t1,t2,t3 Where condi1=5 and condi6=6 or condi7=7 Group by g1,g2
解析后的SQL为
select
c1,c2,c3
from
t1,t2,t3
where
condi1=5 and condi6=6 or condi7=7
group by
g1,g2

原SQL为Select c1,c2,c3 From t1,t2,t3 Where condi1=5 and condi6=6 or condi7=7 Group by g1,g2,g3 order by g2,g3
解析后的SQL为
select
c1,c2,c3
from
t1,t2,t3
where
condi1=5 and condi6=6 or condi7=7
group by
g1,g2,g3
order by
g2,g3


解析效果之二(isSingleLine=true):

原SQL为select * from dual
解析后的SQL为
select
*
from
dual

原SQL为SELECT * frOm dual
解析后的SQL为
select
*
from
dual

原SQL为Select C1,c2 From tb
解析后的SQL为
select
C1,
c2
from
tb

原SQL为select c1,c2 from tb
解析后的SQL为
select
c1,
c2
from
tb

原SQL为select count(*) from t1
解析后的SQL为
select
count(*)
from
t1

原SQL为select c1,c2,c3 from t1 where condi1=1
解析后的SQL为
select
c1,
c2,
c3
from
t1
where
condi1=1

原SQL为Select c1,c2,c3 From t1 Where condi1=1
解析后的SQL为
select
c1,
c2,
c3
from
t1
where
condi1=1

原SQL为select c1,c2,c3 from t1,t2 where condi3=3 or condi4=5 order by o1,o2
解析后的SQL为
select
c1,
c2,
c3
from
t1,
t2
where
condi3=3 or
condi4=5
order by
o1,
o2

原SQL为Select c1,c2,c3 from t1,t2 Where condi3=3 or condi4=5 Order by o1,o2
解析后的SQL为
select
c1,
c2,
c3
from
t1,
t2
where
condi3=3 or
condi4=5
order by
o1,
o2

原SQL为select c1,c2,c3 from t1,t2,t3 wher www.hnnedu.com e condi1=5 and condi6=6 or condi7=7 group by g1,g2
解析后的SQL为
select
c1,
c2,
c3
from
t1,
t2,
t3
where
condi1=5 and
condi6=6 or
condi7=7
group by
g1,
g2

原SQL为Select c1,c2,c3 From t1,t2,t3 Where condi1=5 and condi6=6 or condi7=7 Group by g1,g2
解析后的SQL为
select
c1,
c2,
c3
from
t1,
t2,
t3
where
condi1=5 and
condi6=6 or
condi7=7
group by
g1,
g2

原SQL为Select c1,c2,c3 From t1,t2,t3 Where condi1=5 and condi6=6 or condi7=7 Group by g1,g2,g3 order by g2,g3
解析后的SQL为
select
c1,
c2,
c3
from
t1,
t2,
t3
where
condi1=5 and
condi6=6 or
condi7=7
group by
g1,
g2,
g3
order by
g2,
g3


使用的类SqlParser,你可以拷贝下来使用之:

package com.sitinspring.common.sqlFormatter;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* SQL语句解析器类
* @author: sitinspring(junglesong@gmail.com)
* @date: 2008-3-12
*/
public class SqlParser{
/**
* 逗号
*/
private static final String Comma = ",";

/**
* 四个空格
*/
private static final String FourSpace = " ";

/**
* 是否单行显示字段,表,条件的标识量
*/
private static boolean isSingleLine=true;

/**
* 待解析的SQL语句
*/
private String sql;

/**
* SQL中选择的列
*/
private String cols;

/**
* SQL中查找的表
*/
private String tables;

/**
* 查找条件
*/
private String conditions;

/**
* Group By的字段
*/
private String groupCols;

/**
* Order by的字段
*/
private String orderCols;

/**
* 构造函数
* 功能:传入构造函数,解析成字段,表,条件等
* @param sql:传入的SQL语句
*/
public SqlParser(String sql){
this.sql=sql.trim();

没有正则表达式LIKE

确定给定的字符串是否与指定的模式匹配。模式可以包含常规字符和通配符字符。模式匹配过程中,常规字符必须与字符串中指定的字符完全匹配。然而,可使用字符串的任意片段匹配通配符。与使用 = 和 != 字符串比较运算符相比,使用通配符可使 LIKE 运算符更加灵活。如果任何参数都不属于字符串数据类型,Microsoft® SQL Server™ 会将其转换成字符串数据类型(如果可能)。

语法
match_expression [ NOT ] LIKE pattern [ ESCAPE escape_character ]

参数
match_expression 任何字符串数据类型的有效 SQL Server 表达式。
patternmatch_expression 中的搜索模式,可以包含下列有效 SQL Server 通配符

1 % 包含零个或更多字符的任意字符串
示例:WHERE title LIKE '%computer% ' 将查找处于书名任意位置的包含单词 computer 的所有书名。

2 _(下划线) 任何单个字符
示例:WHERE au_fname LIKE '_ean ' 将查找以 ean 结尾的所有 4 个字母的名字(Dean、Sean 等)。

3 [] 指定范围中的任何单个字符
示例:WHERE au_lname LIKE '[C-P]arsen ' 将查找以arsen 结尾且以介于 C 与 P 之间的任何单个字符开始的 作者姓氏,例如,Carsen、Larsen、Karsen 等

4 [^] 不属于指定范围中的任何单个字符,与 [] 相反
示例:WHERE au_lname LIKE 'de[^l]% ' 将查找以 de 开始且其后的字母不为 l 的所有作者的姓氏。

一 使用 like 的模式匹配:
在搜索Datetime类型时,建议使用like .
LIKE 支持 ASCII 模式匹配和 Unicode 模式匹配。当所有参数,包括 match_expression、pattern 和 escape_character(如果有)都是 ASCII 字符数据类型时,将执行 ASCII 模式匹配。如果其中任何参数属于 Unicode 数据类型,则所有参数将被转换为 Unicode 并执行 Unicode 模式匹配。当对 Unicode 数据(nchar 或 nvarchar 数据类型)使用 LIKE 时,尾随空格是有意义的。但是对于非 Unicode 数据,尾随空格没有意义。Unicode LIKE 与 SQL-92 标准兼容。ASCII LIKE 与 SQL Server 的早期版本兼容

二 使用 % 通配符

例如,此查询将显示数据库中所有的系统表,因为它们都以字母 sys 开始:
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME LIKE 'sys% '
若要查阅非系统表的所有对象,请使用 NOT LIKE 'sys% '。如果共有 32 个对象且 LIKE 找到 13 个与模式匹配的名称,则 NOT LIKE 将找到 19 个与 LIKE 模式不匹配的对象

三 使用escape字句的模式匹配

可搜索包含一个或多个特殊通配符的字符串。例如,customers 数据库中的 discounts 表可能存储含百分号 (%) 的折扣值。若要搜索作为字符而不是通配符的百分号,必须提供 ESCAPE 关键字和转义符。例如,一个样本数据库包含名为 comment 的列,该列含文本 30%。若要搜索在 comment 列中的任何位置包含字符串 30% 的任何行,请指定由 WHERE comment LIKE '%30!%% ' ESCAPE '! ' 组成的 WHERE 子句。如果不指定 ESCAPE 和转义符,SQL Server 将返回所有含字符串 30 的行。

下例使用 ESCAPE 子句和转义符查找 mytbl2 表的 c1 列中的精确字符串 10-15%

USE pubs
GO
IF EXISTS(SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = 'mytbl2 ')
DROP TABLE mytbl2
GO
USE pubs
GO
CREATE TABLE mytbl2
(
c1 sysname
)
GO
INSERT mytbl2 VALUES ( 'Discount is 10-15% off ')
INSERT mytbl2 VALUES ( 'Discount is .10-.15 off ')
GO
SELECT c1
FROM mytbl2
WHERE c1 LIKE '%10-15!% off% ' ESCAPE '! '
GO

四 使用 [] 通配符

下例查找姓为 Carson、Carsen、Karson 或 Karsen 的作者所在的行。
USE pubs
SELECT au_lname, au_fname, phone
FROM authors
WHERE au_lname LIKE '[CK]ars[eo]n '
ORDER BY au_lname ASC, au_fname ASC

sql2005应该可以把sql2000就可以了啊例如下面返回字符串中的汉字:CREATE function chineseRegexReplace( @source varchar(5000), --原字符串 @regexp varchar(1000), --正则表达式 @replace varchar(1000), --替换值 @globalReplace bit = 0, --是否是全局替换 @ignoreCase bit = 0 --是否忽略大小写 ) returnS varchar(1000) AS begin declare @hr integer declare @objRegExp integer declare @result varchar(5000) exec @hr = sp_OACreate 'VBScript.RegExp', @objRegExp OUTPUT IF @hr <> 0 begin exec @hr = sp_OADestroy @objRegExp return null end exec @hr = sp_OASetProperty @objRegExp, 'Pattern', @regexp IF @hr <> 0 begin exec @hr = sp_OADestroy @objRegExp return null end exec @hr = sp_OASetProperty @objRegExp, 'Global', @globalReplace IF @hr <> 0 begin exec @hr = sp_OADestroy @objRegExp return null end exec @hr = sp_OASetProperty @objRegExp, 'IgnoreCase', @ignoreCase IF @hr <> 0 begin exec @hr = sp_OADestroy @objRegExp return null end exec @hr = sp_OAMethod @objRegExp, 'Replace', @result OUTPUT, @source, @replace IF @hr <> 0 begin exec @hr = sp_OADestroy @objRegExp return null end exec @hr = sp_OADestroy @objRegExp IF @hr <> 0 begin return null end return @result endCREATE FUNCTION Str_GetChineseWords(@OldStr varchar(200)) --返回字符串中的汉字RETURNS varchar(200) AS BEGIN declare @New varchar(200) set @New=dbo.chineseRegexReplace(@OldStr,'[^\u3400-\u9FFF]','',1,1) return @NewEND

用全文检索,是最快的

好像不行 用正则表示式是用sql语句速度的10+倍 为什么不用正则呢

不可以

可以用程序来辅助


南康市19562182302: 如何在sql语句中使用正则表达式 -
戴倩热毒: sqlserver中,主要有regexp_like,regexp_replace,regexp_substr,regexp_instr四个正则表达式函数.1、regexp_like:regexp_like(x,pattern[,match_option]),查看x是否与pattern相匹配,该函数还可以提供一个可选的参数match_option字符串说明默...

南康市19562182302: sql 实现正则表达式 -
戴倩热毒: 没有正则表达式 LIKE 确定给定的字符串是否与指定的模式匹配.模式可以包含常规字符和通配符字符.模式匹配过程中,常规字符必须与字符串中指定的字符完全匹配.然而,可使用字符串的任意片段匹配通配符.与使用 = 和 != 字符串比较...

南康市19562182302: 在ms sql中如何使用正则表达式,请给出简单示例,注释越详细越好!感激不尽 -
戴倩热毒: MS SQL不支持正则表达式,可以用CLR实现.1、 新建一个 MSSQL的数据库项目,配置到你的数据库中2、 在数据库项目中新建一个函数库,编写如下代码: /// <summary> /// 验证是否符合正则表达式 /// </summary> [SqlFunction] public static ...

南康市19562182302: 怎么用正则表达式解析sql语句 -
戴倩热毒: 先看要解析的样例SQL语句:select * from dual SELECT * frOm dual Select C1,c2 From tb select c1,c2 from tb select count(*) from t1 select c1,c2,c3 from t1 where condi1=1 Select c1,c2,c3 From t1 Where condi1=1 select c1,c2,c3 from t1,t2 where ...

南康市19562182302: SQL 里面能用正则表达式吗 -
戴倩热毒: 每个数据库都有自己比较特殊的正则表达式(起码oracle有),和java的正则不一样的,所以具体有没有要查查看.就算有也肯定和程序的正则在写法上是不同的.

南康市19562182302: SQL中可用正则表达式不 -
戴倩热毒: 当然可以,具体可查找sql字符串函数. 如mysql的语句:mysql> SELECT 'fo\nfo' REGEXP '^fo$';

南康市19562182302: Db2 中的sql 怎样实现正则表达式的功能 -
戴倩热毒: 尽管上面的函数按照预期的方式工作,但还可以改进它以获得更佳的性能.注:函数内部的执行完成得越快,DB2 处理整个 SQL 语句的速度也就越快.SQL 旨在处理多组行,这意味着通常会针对一个模式匹配多个行.在大多数情况下,模式...

南康市19562182302: sql server中可以用正则表达式吗 -
戴倩热毒: SQLSERVER 2005已经可以支持正则表达式(通过外部用CLR语言(C# .NET、VB.NET)编写过程、触发器和函数),但是SQL2000不支持,如果一定要用,可以用调用VB的正则表达式控件.

南康市19562182302: SQL语句用正则表达式怎样表示? -
戴倩热毒: 正则表达式语法 一个正则表达式就是由普通字符(例如字符 a 到 z)以及特殊字符(称为元字符)组成的文字模式.该模式描述在查找文字主体时待匹配的一个或多个字符串.正则表达式作为一个模板,将某个字符模式与所搜索的字符串进行匹...

南康市19562182302: sybase SQL中怎么实现正则表达式匹配 -
戴倩热毒: CREATE FUNCTION dbo.find_regular_expression( @source varchar(5000), --需要匹配的源字符串 @regexp varchar(1000), --正则表达式 @ignorecase bit = 0 --是否区分大小写,默认为false) RETURNS bit --返回结果0-false,1-true AS BEGIN--...

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网