Sub fff()
  Dim oReg  As RegExp 'As VBScript_RegExp_55.RegExp
  Set oReg = New RegExp
  With oReg
    .Pattern = "^((0|[1-9][0-9]*)([-+*/](0|[1-9][0-9]*))*[-+*/]?)*(((\()((0|[1-9][0-9]*)+([+*/-](0|[1-9][0-9]*))*)+[-+*/]?)+((\))([-+*/](0|[1-9][0-9]*)[-+*/]?)*)+)*((0|[1-9][0-9]*)([-+*/](0|[1-9][0-9]*))*)*$"
    s = "1+6*(2+(3-1)/1+(9-5*1))))"
    Debug.Print .Test(s)
  End With
End Sub
问题是不管正确与否,Debug.print .
s = "1+6*(2+(3-1)/1+(9-5*1))"
s = "1+6*(2+(3-1)/1+(9-5*1)"
s = "1+6*(2+(3-1)/1+(9-5*1))))"Debug.Print oReg.Test(s)的结果总是True
***********************
程序依据JS的括号配对问题http://topic.csdn.net/u/20090719/22/84ecbe48-2bab-4d7f-8222-3cb7f232a451.html?seed=308770229&r=79188478#r_79188478
请问,各位高手正则表达式如何判断括号配对的正确性。谢谢。

解决方案 »

  1.   

    学习中,正则总写不对咯,不过你应该像HTML那样成对描述.
      

  2.   

    ExpressionEvaluato
    能做为vb插件应用吗?
    如何使用,有相关的介绍吗?谢谢。
      

  3.   

    http://kb.cnblogs.com/a/1501701/http://wenda.tianya.cn/wenda/thread?tid=2f220babad96cbdc&hl=zh-CN%3Flid%3Ftid%3Flid%3Ftid%3Flidhttp://download.csdn.net/download/yunzhongyun151/4082630import java.io.*;
    import java.util.Stack;
    public class Analyst {
    public char str[];
    public Stack OPTR=new Stack();          //存放运算符
    public Stack<String> OPND=new Stack<String>();          //存放操作数或结果
    int count=0;
    public int [][] list=new int [][]       //算符优先级表,1是>,-1是<,0是=,2是无优先级
    {
    {1,1,-1,-1,-1,1,-1,1},          //+的下标0
    {1,1,-1,-1,-1,1,-1,1},          //-的下标1
    {1,1,1,1,-1,1,-1,1},            //*的下标2
    {1,1,1,1,-1,1,-1,1},            ///的下标3
    {-1,-1,-1,-1,-1,0,-1,2},        //(的下标4
    {1,1,1,1,2,1,2,1},              //)的下标5
    {1,1,1,1,2,1,2,1},              //i的下标6
    {-1,-1,-1,-1,-1,2,1,0}     //#的下标7
    };

        Analyst(char str[])
        {
            this.str=str;
        }
        public void analyst()
     {
    int i = 0;
    int a = 0, b = 0;
    String s = "";
    OPTR.push("#");
    while (i < str.length) // 依次读入表达式的单词
    {

    char temp = str[i]; // 记录当前输入串
    System.out.print("测试1:");
    System.out.println(temp);
    if (!isoperator(temp)) // 一直判断到下一元素是操作符,否则一直是数字串
    {
    s = s + str[i];
    i++;
    } else 
    {
    OPND.push(s); // 是操作符就将s数字串压进OPND栈
    System.out.print("测试2:");
    if(i==str.length)
    if (isoperator(temp) && isoperator(str[i + 1])) 
    {
     System.out.println("输出:   The expression is error!");
    }

        else 
        {
    char tem = OPTR.peek().toString().charAt(0); // 取出OPTR栈顶操作符
    int m, n;
    n = operator(temp); // 得到当前操作符的下标
    m = operator(tem); // 得到栈顶操作符的下标
    int l = list[m][n]; // 获得优先级的高低
    System.out.print(temp); // 测试
    System.out.println(tem);
    System.out.println(l);
    if (l == -1) {
    OPTR.push(temp);
    i++;
    } else {
    if (l == 0) {
    if (temp == '#') {
    System.out.println("表达式语法分析成功:");
    System.out.println("结果:");
    System.out.println(OPND.peek().toString());
    OPTR.pop();
    i++;
    } else {
    System.out.println("pop:" + OPTR.pop());
    }
    } else {
    if (l == 1) {
    int r = 0;
    int li;
    char t;
    b = Integer.parseInt((String) OPND.pop());
    System.out.println("b=" + b);
    a = Integer.parseInt((String) OPND.pop());
    System.out.println("a=" + a);
    t = OPTR.pop().toString().charAt(0);
    li = operator(t);
    if (li == 0) //
    {
    r = a + b;
    System.out.println(r);
    } else {
    if (li == 1) {
    r = a - b;
    } else {
    if (li == 2) {
    r = a * b;
    } else {
    if (li == 3) {
    r = a / b;
    }
    }
    }
    }
    System.out.println(r); String s2 = String.valueOf(r);
    OPND.push(s2);
    }
    }
    } }

    }
    }
    private int operator(char temp) {
    // TODO Auto-generated method stub
    switch(temp)
    {
    case '+':
    return 0;
    case '-':
    return 1;
    case '*':
    return 2;
    case '/':
    return 3;
    case '(':
    return 4;
    case ')':
    return 5;
    case  'i':
    return 6;
    case '#':
    return 7;
    default :
    return 8;
    }

    }
    private boolean isoperator(char temp) {
    if(temp=='+'||temp=='-'||temp=='*'||temp=='/'||temp=='('||temp==')'||temp=='#')
    return true;
    // TODO Auto-generated method stub
    return false;
    }
    }