您的位置:程序门 -> .net技术 -> c#



请高手指点一下:一个递归式的正则表达式的写法


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


请高手指点一下:一个递归式的正则表达式的写法[已结贴,结贴人:mymedia]
发表于:2007-01-23 15:51:10 楼主
请高手指点一下    
 
totalspot*(totalspot*(totalspot*(mediaunitcost*(1-medianegotiationdiscount)*(1-othermediadiscount)*(1-agency     discount)*(1+medianegotiationloading)*(1+othermedialoading))))    
 
怎么样写出一个匹配递归式的正则表达式    
 
要求:    
匹配所有括号里面的内容,    
如:(1+medianegotiationloading),    
(mediaunitcost*(1-medianegotiationdiscount)*(1-othermediadiscount)*(1-agency     discount)*(1+medianegotiationloading)*(1+othermedialoading)),    
(totalspot*(mediaunitcost*(1-medianegotiationdiscount)*(1-othermediadiscount)*(1-agency     discount)*(1+medianegotiationloading)*(1+othermedialoading))),    
(totalspot*(totalspot*(mediaunitcost*(1-medianegotiationdiscount)*(1-othermediadiscount)*(1-agency     discount)*(1+medianegotiationloading)*(1+othermedialoading))))    
 
等.....
发表于:2007-01-23 15:52:311楼 得分:0
想了一天
难就一个字
发表于:2007-01-23 15:54:142楼 得分:0
有情幫頂
发表于:2007-01-23 15:55:233楼 得分:0
是只匹配()     还是(())   ((()))都要匹配?
发表于:2007-01-23 15:58:224楼 得分:0
都要匹配
发表于:2007-01-23 15:58:435楼 得分:0
谢谢各位
发表于:2007-01-23 15:59:496楼 得分:0
你是要干什么啊?说说具体要做什么   也许不需要用正则
发表于:2007-01-23 16:04:597楼 得分:0
处理一个公式的计算     要判断数据是否是百分数     还是一个总数         还要提取前面的符号
公式是任意定义的!!!!
例如说:
a=b+c;
b=d+e
d=f+g

那么a=((f+g)+e)+c

这是简单的一个case,只有用到加号.....郁闷ing...........
发表于:2007-01-23 16:14:328楼 得分:0
如果是百分数就直接取代

如果是数目就要汇率转换后,由总数加上这个变量前面的运算符号

真实的例子:
amount=totalspot*(mediaunitcost*(1-medianegotiationdiscount)*(1-othermediadiscount)*(1-agency   discount)*(1+medianegotiationloading)*(1+othermedialoading))

括号内含有1,例如(1-medianegotiationdiscount),
输入当前数值是medianegotiationdiscount,
如果数值是5%,则直接替换,amount=totalspot*(mediaunitcost*(1-5%)*(1-othermediadiscount)*(1-agency   discount)*(1+medianegotiationloading)*(1+othermedialoading));
如果数值是5,则转换汇率后是50;
则最终结果是:amount=totalspot*(mediaunitcost*(1-0)*(1-othermediadiscount)*(1-agency   discount)*(1+medianegotiationloading)*(1+othermedialoading))-50

公式是任意的......

发表于:2007-01-23 16:34:329楼 得分:0
看了很久,还没明白要的是什么
发表于:2007-01-23 16:37:2910楼 得分:0
要的是:

在给出一个字符串里面,找出所有配对括号里面的内容;
例如:
字符串是:totalspot*(totalspot*(totalspot*(mediaunitcost*(1-medianegotiationdiscount)*(1-othermediadiscount)*(1-agency   discount)*(1+medianegotiationloading)*(1+othermedialoading))))

就要得到的是:(1+othermedialoading),(1+medianegotiationloading),...
(mediaunitcost*(1-medianegotiationdiscount)*(1-othermediadiscount)*(1-agency   discount)*(1+medianegotiationloading)*(1+othermedialoading)),...
(totalspot*(mediaunitcost*(1-medianegotiationdiscount)*(1-othermediadiscount)*(1-agency   discount)*(1+medianegotiationloading)*(1+othermedialoading))),...
(totalspot*(totalspot*(mediaunitcost*(1-medianegotiationdiscount)*(1-othermediadiscount)*(1-agency   discount)*(1+medianegotiationloading)*(1+othermedialoading))))...
等一个数组
发表于:2007-01-23 18:19:2911楼 得分:50
兄弟,我给你取出来了,虽然方法有点那个~恶,便毕竟是取出来了,下面是我的测试代码,你试下吧,至于里面方法有更简洁的你自己处理一下吧,还有,我没有存数组,这个应该就不难了吧,唉,一个多小时的青春啊

string   yourstr=richtextbox1.text   ;                 //你的源字符串
matchcollection   mc   =   regex.matches(yourstr,@ "\([^\(\)]+?\) ",regexoptions.ignorecase);                           //这个是用来取单一配对括号里的内容
foreach(match   m   in   mc)
{
          richtextbox2.text   +=   m.groups[0].value+ "\n ";                 //这是提取内容
}
string   start   =   " ";                                         //下面为取多重括号内的内容
string   end   =   " ";
int   len   =   regex.replace(yourstr, ".*(? <!\\))(.*)$ ", "$1 ",   regexoptions.ignorecase).length;       //这个是取最后“)”的数量,应该是有更好的方法,反正我是一起用正则了
for(int   i=1;i <len;i++)
{
        match   m   =   regex.match(yourstr,   start+@ "\(([^\(\)]+?\([^\(\)]+?\))+?\) "+end,   regexoptions.ignorecase);
        richtextbox2.text   +=   m.groups[0].value+ "\n ";                 //这是提取内容
        start   +=@ "\([^\(\)]+? ";
        end   +=     @ "\) ";
}
发表于:2007-01-24 10:07:2012楼 得分:50
string   a= "(totalspot*(totalspot*(mediaunitcost*(1-medianegotiationdiscount)*(1-othermediadiscount)*(1-agency   discount)*(1+medianegotiationloading)*(1+othermedialoading)))) ";
int   icount=a.length;
arraylist   alist=new   arraylist();
for(int   i=0;i <icount;i++)
{
if(a[i].equals( '( '))
{
int   ib=1,ie=0,j=0;
for(j=i+1;j <icount;j++)
{
if(a[j].equals( '( '))
ib++;
if(a[j].equals( ') '))
ie++;
if(ib==ie)
break;
}
alist.add(a.substring(i,j-i+1));
}
}
string[]   stemps=(string[])alist.toarray(typeof(string));//转数组,用alist可不转
//不要意思,昨天开会,没来的急回你   这样简单处理就可以了,而且比正则的效率高出很多
//应该能适应所有情况


快速检索

最新资讯
热门点击