当前位置:首页 » 编程语言 » 正则表达式详解sql
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

正则表达式详解sql

发布时间: 2022-07-29 01:21:11

⑴ php正则表达式 解析sql

$sql='
CREATE TABLE IF NOT EXISTS uploadtype(
id int(11) NOT NULL AUTO_INCREMENT,
title varchar(20) DEFAULT '0',
sydefault char(1) DEFAULT '0' ,
PRIMARY KEY (id)
) ENGINE=MyISAM
';
preg_match('#CREATE TABLE.*\(.*\)ENGINE=MyISAM#isU',$sql,$typefile);
var_mp($typefile);

⑵ 正则表达式如何解析sql语句

哇,你们老师一定是希望你们用ibatis吧,这个就是xml里面写sql,然后你们老师好不同哦,要求好严格哦,我不喜欢哦,替我转告一下啊
insert语句呀,indexOf就够了,还正则什么呀
map里面随便储存啊,什么类型都可以啦,String嘛啊

⑶ SQL正则表达式(做注册时的备注)要详解。带语法。规定非空验证与不少于10个字符。

如果是SQL里的字段,那么:字段名 varchar(50) not null check(len(字段名) >= 10) 如果是在html页面,那么用变量的length属性来判断长度即可。

⑷ java 怎么用正则表达式解析sql中的表名,已有半成品,求改善

Stringstr="from\s+(.*)\s+where?";
Stringsql="select*fromtable,table2wherea=b";
Patternp=Pattern.compile(str);
Matchermatcher=p.matcher(sql);
while(matcher.find()){
Stringstring=matcher.group(1);
System.out.println(string);
}

⑸ sql正则表达式常用符号

SQL的查询语句中,有时会需要引进正则表达式为其复杂搜索指定模式。下面给出一些Regexp在
MYSQL语句中应用(非全部):
1) ^
匹配字符串的开始部分。
mysql> SELECT 'fo\nfo' REGEXP '^fo$'; -> 0mysql> SELECT 'fofo' REGEXP '^fo'; -> 12) $
匹配字符串的结束部分。
mysql> SELECT 'fo\no' REGEXP '^fo\no$'; -> 1mysql> SELECT 'fo\no' REGEXP '^fo$'; -> 03) .
匹配任何字符(包括回车和新行)。
mysql> SELECT 'fofo' REGEXP '^f.*$'; -> 1mysql> SELECT 'fo\r\nfo' REGEXP '^f.*$'; -> 14)
[:character_class:]
在括号表达式中(使用[和]),[:character_class:]表示与术语类的所有字符匹配的字符类。标准的类名称是:
alnum
文字数字字符
alpha
文字字符
blank
空白字符
cntrl
控制字符
digit
数字字符
graph
图形字符
lower
小写文字字符
print
图形或空格字符
punct
标点字符
space
空格、制表符、新行、和回车
upper
大写文字字符
xdigit
十六进制数字字符
它们代表在ctype(3)手册页面中定义的字符类。特定地区可能会提供其他类名。字符类不得用作范围的端点。
mysql> SELECT 'justalnums' REGEXP '[[:alnum:]]+'; -> 1
mysql> SELECT '!!' REGEXP '[[:alnum:]]+'; -> 0
5) [[:<:]], [[:>:]]
这些标记表示word边界。它们分别与word的开始和结束匹配。word是一系列字字符,其前面和后面均没有字
字符。字字符是alnum类中的字母数字字符或下划线(_)。
mysql> SELECT 'a word a' REGEXP '[[:<:]]word[[:>:]]'; -> 1mysql> SELECT 'a xword a' REGEXP
'[[:<:]]word[[:>:]]'; -> 0要想在正则表达式中使用特殊字符的文字实例,应在其前面加上2个反斜杠“\”字符。
MySQL解析程序负责解释其中一个,正则表达式库负责解释另一个。例如,要想与包含特殊字符“+”的字符
串“1+2”匹配,在下面的正则表达式中,只有最后一个是正确的:
mysql> SELECT '1+2' REGEXP '1+2'; -> 0mysql> SELECT '1+2' REGEXP '1\+2'; -> 0mysql> SELECT
'1+2' REGEXP '1\\+2'; -> 1 其他的有关Regexp的语法,可直接参考下表:字符 含意
\ 做为转意,即通常在"\"后面的字符不按原来意义解释,如/b/匹配字符"b",当b前面加了反斜杆后/\b/,转意
为匹配一个单词的边界。
-或-
对正则表达式功能字符的还原,如"*"匹配它前面元字符0次或多次,/a*/将匹配a,aa,aaa,加了"\"后,/a\*/将
只匹配"a*"。
^ 匹配一个输入或一行的开头,/^a/匹配"an A",而不匹配"An a"
$ 匹配一个输入或一行的结尾,/a$/匹配"An a",而不匹配"an A"
* 匹配前面元字符0次或多次,/ba*/将匹配b,ba,baa,baaa
+ 匹配前面元字符1次或多次,/ba*/将匹配ba,baa,baaa
? 匹配前面元字符0次或1次,/ba*/将匹配b,ba
(x) 匹配x保存x在名为$1...$9的变量中
x|y 匹配x或y
{n} 精确匹配n次
{n,} 匹配n次以上
{n,m} 匹配n-m次
[xyz] 字符集(character set),匹配这个集合中的任一一个字符(或元字符)
[^xyz] 不匹配这个集合中的任何一个字符
[\b] 匹配一个退格符
\b 匹配一个单词的边界
\B 匹配一个单词的非边界
\cX 这儿,X是一个控制符,/\cM/匹配Ctrl-M
\d 匹配一个字数字符,/\d/ = /[0-9]/
\D 匹配一个非字数字符,/\D/ = /[^0-9]/
\n 匹配一个换行符
\r 匹配一个回车符
\s 匹配一个空白字符,包括\n,\r,\f,\t,\v等
\S 匹配一个非空白字符,等于/[^\n\f\r\t\v]/
\t 匹配一个制表符
\v 匹配一个重直制表符
\w 匹配一个可以组成单词的字符(alphanumeric,这是我的意译,含数字),包括下划线,如[\w]匹配
"$5.98"中的5,等于[a-zA-Z0-9]
\W 匹配一个不可以组成单词的字符,如[\W]匹配"$5.98"中的$,等于[^a-zA-Z0-9]。

⑹ 在ms sql中如何使用正则表达式,请给出简单示例,注释越详细越好!感激不尽

MSSQL不支持正则表达式,可以用CLR实现。

1、新建一个MSSQL的数据库项目,配置到你的数据库中

2、在数据库项目中新建一个函数库,编写如下代码:

///<summary>

///验证是否符合正则表达式

///</summary>

[SqlFunction]

(stringinput,stringregex)

{

returnnewSqlBoolean(Regex.IsMatch(input,regex,RegexOptions.IgnoreCase));

}

然后在数据库项目上点击“右键”,选择“部署”

PS:此功能需要MSSQL2005或者以上版本支持

如果你使用的是.NET3.5版本的话,需要在数据库服务器上安装.netframework3.5

目前SQLSERVERCLR不支持.NET4.0,所以如果你使用VS2010开发的话需要把项目版本修改成为.NET2.0/3.5

使用方法:

SELECT*FROM[table]WHEREdbo.RegexIsMatch([ID],'^d+$')=1

⑺ 怎么样用正则表达式来解析sql语句

sqlserver中,主要有regexp_like,regexp_replace,regexp_substr,regexp_instr四个正则表达式函数。

⑻ 怎么用正则表达式解析sql语句

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

select * from al
SELECT * frOm al
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 al
解析后的SQL为
select
*
from
al

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

原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 al
解析后的SQL为
select
*
from
al

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

原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.hnne.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([email protected])
* @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 正则表达式

你好
说实话没看懂你的意思
请你给一下数据,并且给一下你需要的结果
欢迎你追问吧,一定帮你搞定

⑽ 如何在sql语句中使用正则表达式

sqlserver中,主要有regexp_like,regexp_replace,regexp_substr,regexp_instr四个正则表达式函数。

1、regexp_like:

regexp_like(x,pattern[,match_option]),查看x是否与pattern相匹配,该函数还可以提供一个可选的参数match_option字符串说明默认的匹配选项。match_option的取值如下:

'c' 说明在进行匹配时区分大小写(缺省值);

'i' 说明在进行匹配时不区分大小写;

'n' (.)点号能表示所有单个字符,包括换行(俺还不知道什么地方有用到换行.只知道sql里面可以用chr(10)表示换行、

'm' 字符串存在换行的时候当作多行处理.这样$就可匹配每行的结尾.不然的话$只匹配字符串最后的位置、

示例:

select * from emp where regexp_like(ename,'^a[a-z]*n$');

可以查找ename中以a开头以n结尾的行.例如ename为arwen或arwin或anden.但Arwen不能被匹配.因为默认是区分大小写.如果是

select * from emp where regexp_like(ename,'^a[a-z]*n$','i')

则可以查找ename为Arwen的行记录。

2、regexp_instr:

REGEXP_INSTR(x,pattern[,start[,occurrence[,return_option[, match_option]]]])用于在x中查找pattern。返回pattern在x中出现的位置。匹配位置从1开始。可以参考字符串函数 INSTR(),参数相关:

'start' 开始查找的位置;

'occurrence' 说明应该返回第几次出现pattern的位置;

'return_option' 说明应该返回什么整数。若该参数为0,则说明要返回的整数是x中的一个字符的位置;若该参数为非0的整数,则说明要返回的整数为x中出现在pattern之后 的字符的位置;

'match_option' 修改默认的匹配设置.与regexp_like里面的相同.

示例:

DECLARE

V_RESULT INTEGER ;

BEGIN

SELECT REGEXP_INSTR('hello world','o',1,1,0) INTO V_RESULT

FROM DUAL;

DBMS_OUTPUT.PUT_LINE(V_RESULT);

END;

结果为5,即字母o第一个次出现的位置。

如果regexp_instr('hello world','o',1,1,n)其中n为除0之外的整数。比如1,3。则结果为6.表示第一次出现字母o的后面一个字符的位置。

如果regexp_instr('hello world','o',1,2,0)则结果为9.表示第二次出现字母o的位置.

3、regexp_replace:

REGEXP_REPLACE(x,pattern[,replace_string[,start[,occurrence[, match_option]]]])用于在x中查找pattern,并将其替换为replae_string。可以参考字符串函数 REPLACE(),参数同REGEXP_INSTR函数

示例:

DECLARE

V_RESULT varchar2(90);

BEGIN

SELECT REGEXP_REPLACE('hello world','o','x',1,1) INTO V_RESULT

FROM DUAL;

DBMS_OUTPUT.PUT_LINE(V_RESULT);

END;

结果为hellx world.

如果REGEXP_REPLACE('hello world','o','x'),则结果为hellx wxrld.

如果 REGEXP_REPLACE('hello world','o','x',1,2)则结果为hello wxrld.

4、regexp_substr:

REGEXP_SUBSTR(x,pattern[,start[,occurrence[, match_option]]])用于在x中查找pattern并返回。可以参考字符串函数 SUBSTR(),参数同REGEXP_INSTR函数.

例如:

DECLARE

V_RESULT VARCHAR2(255);

BEGIN

SELECT REGEXP_SUBSTR('hello world','l{2}') INTO V_RESULT

FROM DUAL;

DBMS_OUTPUT.PUT_LINE(V_RESULT);

END ;

结果为ll

查询到匹配的字符串才返回匹配的字符.没查到就返回空。