110,534
社区成员
发帖
与我相关
我的任务
分享
string str = "(((空调1 > 30)且(空调2 < 30))或((房间温度 < 32)且(门 = 1)))非(门 = 0)";
string ss = str.Replace("(",string.Empty).Replace(")",string.Empty);//替换()为空
int i = ss.IndexOf("非");
string value1 = ss.Substring(i + 1, ss.Length - i - 1);//门 = 0
string s1 = ss.Substring(0, i);//空调1 > 30且空调2 < 30或房间温度 < 32且门 = 1
int j = s1.IndexOf("或");
string 空调 = s1.Substring(0, j);//空调1 > 30且空调2 < 30
int j1 = 空调.IndexOf("且");
string value2 = 空调.Substring(0, j1);//空调1 > 30
string value3 = 空调.Substring(j1 + 1, 空调.Length - j1 - 1);//空调2 < 30
string 房间 = s1.Substring(j + 1, s1.Length-j-1);//空调1 > 30且空调2 < 30
int j2 = 房间.IndexOf("且");
string value4 = 房间.Substring(0, j2);//房间温度 < 32
string value5 = 房间.Substring(j2 + 1, 房间.Length - j2 - 1);//门 = 1
string s = "(((空调1 > 30)且(空调2 < 30))或((房间温度 < 32)且(门 = 1)))非(门 = 0)";
Regex reg = new Regex(@"(?is)(?<=\()[^\)]+(?=\))");
MatchCollection mc = reg.Matches(s);
foreach (Match m in mc)
{
MessageBox.Show(m.Value.Replace("(", string.Empty));
}
相应取出
空调1 > 30
空调2 < 30
房间温度 < 32
门 = 1
门 = 0
--实现split功能 的函数
create function f_split(@SourceSql varchar(8000),@StrSeprate varchar(10))
returns @temp table(a varchar(100))
as
begin
declare @i int
set @SourceSql=rtrim(ltrim(@SourceSql))
set @i=charindex(@StrSeprate,@SourceSql)
while @i>=1
begin
insert @temp values(left(@SourceSql,@i-1))
set @SourceSql=substring(@SourceSql,@i+1,len(@SourceSql)-@i)
set @i=charindex(@StrSeprate,@SourceSql)
end
if @SourceSql<>'\'
insert @temp values(@SourceSql)
return
end
--查询
select * from dbo.f_split('(((空调1 > 30)且(空调2 < 30))或((房间温度 < 32)且(门 = 1)))非(门 = 0)','或') union all
select * from dbo.f_split(replace(replace('(((空调1 > 30)且(空调2 < 30))或((房间温度 < 32)且(门 = 1)))非(门 = 0)','或','-'),'且','-'),'-') union all
select * from dbo.f_split(replace(replace(replace(replace('(((空调1 > 30)且(空调2 < 30))或((房间温度 < 32)且(门 = 1)))非(门 = 0)','或','-'),'且','-'),'(',''),')',''),'-')
结果:
a
----------------------------------------------------------------------------------------------------
(((空调1 > 30)且(空调2 < 30))
((房间温度 < 32)且(门 = 1)))非(门 = 0)
(((空调1 > 30)
(空调2 < 30))
((房间温度 < 32)
(门 = 1)))非(门 = 0)
空调1 > 30
空调2 < 30
房间温度 < 32
门 = 1非门 = 0
(10 行受影响)
把这个直接插入数据库即可。private void DoDebug0001()
{
List<string> oGet = new List<string>();
string sPut = "(((空调1 > 30)且(空调2 < 30))或((房间温度 < 32)且(门 = 1)))非(门 = 0)";
Stack<StringBuilder> stack = new Stack<StringBuilder>();
stack.Push(new StringBuilder(""));
foreach (char cPut in sPut)
{
switch (cPut)
{
case '(':
stack.Peek().Append(cPut);
stack.Push(new StringBuilder(""));
break;
case ')':
StringBuilder oPop = stack.Pop();
oGet.Add(oPop.ToString());
stack.Peek().Append(oPop.ToString());
stack.Peek().Append(cPut);
break;
default:
stack.Peek().Append(cPut);
break;
}
}
while (stack.Count > 0)
{
StringBuilder oPop = stack.Pop();
oGet.Add(oPop.ToString());
}
foreach (string sGet in oGet)
{
Console.WriteLine(sGet);
}
}
输出:
空调1 > 30
空调2 < 30
(空调1 > 30)且(空调2 < 30)
房间温度 < 32
门 = 1
(房间温度 < 32)且(门 = 1)
((空调1 > 30)且(空调2 < 30))或((房间温度 < 32)且(门 = 1))
门 = 0
(((空调1 > 30)且(空调2 < 30))或((房间温度 < 32)且(门 = 1)))非(门 = 0)
class Program
{
static void Main(string[] args)
{
string sInput = "(((空调1 > 30)且(空调2 < 30))或((房间温度 < 32)且(门 = 1)))非(门 = 0)";
string[] arr = sInput.Select(x => x.ToString()).ToArray();
//记录括号的位置
List<Brackets> listBrackets = new List<Brackets>();
//左括号个数
int iLeftNum = 0;
//右括号个数
int iRightNum = 0;
//括号最大层数
int iMaxDeep = 0;
bool bIsErrFormat = false;
for (int i = 0; i < arr.Length; i++)
{
if (arr[i] == "(")
{
iLeftNum++;
listBrackets.Add(new Brackets(true, i));
}
else if (arr[i] == ")")
{
if (iLeftNum - iRightNum > iMaxDeep)
{
iMaxDeep = iLeftNum - iRightNum;
}
iRightNum++;
listBrackets.Add(new Brackets(false, i));
if (iRightNum > iLeftNum)
{
bIsErrFormat = true;
break;
}
}
}
//简单验证
if (bIsErrFormat || (iLeftNum != iRightNum))
{
Console.WriteLine("括号不匹配");
return;
}
//分析括号
List<Condition> lstCondition = new List<Condition>();
string sCondition1 = "";
string sCondition2 = "";
string sRelation = "";
int iLastRight = 0;
//逐层取出条件
for (int iDeep = iMaxDeep; iDeep > 0; iDeep--)
{
sCondition1 = "";
sCondition2 = "";
sRelation = "";
iLastRight = 0;
//左括号个数
iLeftNum = 0;
//右括号个数
iRightNum = 0;
for (int i = 0; i < listBrackets.Count; i++)
{
Brackets obj = listBrackets[i];
if (obj.bIsLeft)
{
iLeftNum++;
}
else
{
if (iLeftNum - iRightNum == iDeep)
{
int iLeft = listBrackets[i - 1].iIndexOfStr;
int iRight = obj.iIndexOfStr;
string sCondition = sInput.Substring(iLeft + 1, iRight - iLeft - 1);
if (string.IsNullOrEmpty(sCondition1))
{
sCondition1 = sCondition;
iLastRight = iRight;
}
else
{
sCondition2 = sCondition;
sRelation = sInput.Substring(iLastRight + 1, iLeft - iLastRight - 1);
lstCondition.Add(new Condition(sCondition1, sCondition2, sRelation));
//移除这对条件
listBrackets.RemoveRange(i - 3, 4);
i -= 4;
sCondition1 = "";
}
}
iRightNum++;
}
}
}
//显示结果
foreach (Condition item in lstCondition)
{
Console.WriteLine("[{0}]{1}[{2}]", item.sCondition1, item.sRelation, item.sCondition2);
}
Console.ReadLine();
}
}
/// <summary>
/// 括号
/// </summary>
public class Brackets
{
/// <summary>
/// 是否是左括号
/// </summary>
public bool bIsLeft;
/// <summary>
/// 在字符串中的位置
/// </summary>
public int iIndexOfStr;
public Brackets(bool bIsLeft, int iIndexOfStr)
{
this.bIsLeft = bIsLeft;
this.iIndexOfStr = iIndexOfStr;
}
}
/// <summary>
/// 条件
/// </summary>
public class Condition
{
public string sCondition1;
public string sCondition2;
public string sRelation;
public Condition(string sCondition1, string sCondition2, string sRelation)
{
this.sCondition1 = sCondition1;
this.sCondition2 = sCondition2;
this.sRelation = sRelation;
}
}