现作一组合查询,生成一字串,a,b,c,d,e 等代表条件
例如:a and b and c or d and e or f and g and h
经过转换变成 a and b and (c or d) and (e or f) and g and h
条件是随机生成得还可以是
例如:a or b and c and d or e
经过转换变成 (a or b) and c and (d or e)
我分析
只要出现 and ... or 则在and后面加(,如果出现 or....and 则加)
而第二种情况先判断首先出现得是and 还是or 对应处理,但是不知用asp该如何实现
请高手指点,最好有代吗明示。
必给分,谢谢,
---------------------------------------------------------------
如果你的意思是说要用VBScript来作这个转换,那么很简单,用正则表示式就行了,譬如(该例子还把a or b or c and e转换成(a or b or c) and e)
1<script language="vbscript">
2function Transform(str)
3DIM oRegExp
4set oRegExp = new RegExp
5oRegExp.Global = TRUE
6oRegExp.IgnoreCase = TRUE
7oRegExp.Pattern = "(\w+(\s+OR\s+\w+)+)"
8Transform = oRegExp.Replace(str,"($1)")
9Set oRegExp = nothing
10end function
11
12dim str
13str = "a and b and c or d and e or f and g and h"
14msgbox "original:" & chr(13) & chr(10) & str
15msgbox "converted:" & chr(13) & chr(10) & Transform(str)
16
17str = "a or b and c and d or e"
18msgbox "original:" & chr(13) & chr(10) & str
19msgbox "converted:" & chr(13) & chr(10) & Transform(str)
20
21str = "a or b or c and d and e or f and g"
22msgbox "original:" & chr(13) & chr(10) & str
23msgbox "converted:" & chr(13) & chr(10) & Transform(str)
24</script>
---------------------------------------------------------------
对含有中文的就失效了,改为
1<script language="vbscript">
2function Transform(str)
3arr=split(str," and ")
4for i=lbound(arr) to ubound(arr)
5if instr(arr(i)," or ")>0 then arr(i)="(" & arr(i) & ")"
6next
7Transform=join(arr," and ")
8end function
9
10dim str
11str = "a='中文Abc' and b='中文Abc' and c='中文Abc' or d='中文Abc' and e=='中文Abc' or f=='中文Abc' and g='中文Abc' and h='中文Abc'"
12msgbox str & chr(13) & chr(10) & Transform(str)
13
14str = "a='中文Abc' or b='中文Abc' and c='中文Abc' and d='中文Abc' or e='中文Abc'"
15msgbox str & chr(13) & chr(10) & Transform(str)
16
17str = "a='中文Abc' or b='中文Abc' or c='中文Abc' and d='中文Abc' and e='中文Abc' or f='中文Abc' and g='中文Abc'"
18msgbox str & chr(13) & chr(10) & Transform(str)
19</script>