string str="(你好)abcdefg"
string str2="(fiejfepfjpx!ij)abcsomthing什么"/*
 想要的效果是,不管str怎么变化,我只需它句子中,括号内的内容,其它的都不需要.
  
 如 temp=fx(str)
    结果: 你好
    temp=fx(str2)
    结果:fiejfepfjpx!ij*/
如何写这个方法 fx(string x) 呢?

解决方案 »

  1.   

            string fx(string s)
            {
                return Regex.Match(s, @"\((.+?)\)").Groups[1].Value;
            }
      

  2.   


    为什么要分组?Regex.Match(s, @"\((.+?)\)").Value
      

  3.   

    Regex r = new Regex(@"[(]([^)]*)[)]", RegexOptions.IgnoreCase);
    Match m = r.Match(content);
    if(m.Success)
    {
    Console.WriteLine(m.Groups[0]);
    }
      

  4.   


    //Try it.
            private String fx(string x) 
            {
                Regex objRegex = new Regex(@"(?<=\()[^\(\)]+(?=\))");
                String resutl;
                Match m = objRegex.Match(x);
                if (m.Success)
                {
                    resutl = m.Value;
                }
                else 
                {
                    resutl = null;
                }
                return resutl;
            }
      

  5.   

    ls的这一段
     else 
     {
        resutl = null;
     }
    就多于了
      

  6.   

    更喜欢自己写一个这种特定的
    第1次获取括号右边Substring(0,“)”); 
    第2次获取括号左边Substring(“(”); 
    这样看起来很清楚
      

  7.   

    try...
      static void Main(string[] args)
            {
                string str = "(fiejfepfjpx!ij)abcsomthing什么";
                Regex re = new Regex(@"((?<=\()[^)]+(?=\)))");
                if(re.Match(str).Success)
                    Console.WriteLine(re.Match(str).Value);
            }
      

  8.   

    (/[^\(]+(?=\))/g
    string reg = @"\d(\d)\d";