⑴ 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
查詢到匹配的字元串才返回匹配的字元.沒查到就返回空。