代码之家  ›  专栏  ›  技术社区  ›  Lloyd Powell binku

字符串格式或正则表达式

  •  1
  • Lloyd Powell binku  · 技术社区  · 15 年前

    我需要一个简单的方法来检查发送到我的函数的字符串是否为以下形式:

    (x + n)(x + m) 
    //the +'s can be minus'
    //n and m represent a double
    //x represents the char 'x'
    

    有没有一个简单的字符串格式,我可以用来检查这是表单。而不是单独检查每个字符。

    空格将被删除以避免混淆。

    当做

    劳埃德

    4 回复  |  直到 15 年前
        1
  •  4
  •   jessegavin    15 年前

    下面是一个正则表达式示例…

    var pattern = @"^(\(x[+-]\d+(\.\d+)?\)){2}$";
    var input = "(x-0.123)(x+5)";
    var result = System.Text.RegularExpressions.Regex.IsMatch(input, pattern);
    
    if (result) { 
      Console.Write("YAY IT MATCHES");
    }
    
        2
  •  2
  •   Andrew Bezzub    15 年前

    你可以使用 regular expressions 来检查这个。

        3
  •  1
  •   Community CDub    7 年前

    在这种情况下,我认为正则表达式很有意义。与C++不同,C语言没有一种方式(我知道)使用字符串格式化作为解析字符串的一部分。

    Quoting Eric Lippert :

    这聪明吗?不。漂亮吗?不。 短?不,根据 规范?

    希望如此,但我还没有完全测试过。 不过看起来不错。

        static bool testJoin(string x)
        {
            string[] s = x.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            if (s.Length != 3) return false;
            if (s[1] != "+" && s[1] != "-") return false;
            if (s[0] != "x") return false;
            double tmp;
            return Double.TryParse(s[2], out tmp);
        }
        static bool testString(string x)
        {
            if (x.Length < 2) return false;
            if (x[0] != '(' || x[x.Length-1]!=')') return false;
            string[] y = x.Substring(1,x.Length-2).Split(new string[] { ")(" }, StringSplitOptions.None);
            if (y.Length != 2) return false;
            return testJoin(y[0]) && testJoin(y[1]);
        }
    
        4
  •  1
  •   Pratik Deoghare    15 年前

    !正则表达式

    使用这个就不会有两个问题;)

     class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine(IsGood1("+x(3)-)x-5+"));
                Console.WriteLine(IsGood1("(x * n)(x + m)"));
                Console.WriteLine(IsGood1(" ( x + 12.9) (x+33.9)"));
            }
    
            private static bool IsOrdered(string s) // bad idea
            {
                var ind = new List<int>();
                ind.Add(s.IndexOf('('));
                ind.Add(s.IndexOfAny(new char[] { '+', '-' }));
                ind.Add(s.IndexOf(')'));
                ind.Add(s.LastIndexOf('('));
                ind.Add(s.LastIndexOfAny(new char[] { '+', '-' }));
                ind.Add(s.LastIndexOf(')'));
    
                bool order = true;
    
                for (int i = 0; i < ind.Count - 1; i++)
                {
                    order = order && (ind[i] < ind[i + 1]);
                }
                return order;
            }
    
            public static bool IsGood1(string s)
            {
                if (!IsOrdered(s)) return false;
    
                double m = 0;
                int c = 0;
    
                foreach (var item in s.Split(new char[] { '+', '-', '(', ')' }))
                {
                    var xx = item.Trim();
    
                    if (xx != "")
                        switch (c)
                        {
                            case 0:
                            case 2:
                                if (xx == "x") c++;
                                break;
                            case 1:
                            case 3:
                                if (double.TryParse(xx, out m)) c++;
                                break;
                        }
                }
    
                return c == 4;
            }
    
        }