例如 abcdef、yuybeiusdf、biookkde这三个字符串都有be,如何写正则表达式来匹配?

解决方案 »

  1.   

    using System;
    using System.Collections;
    using System.Text.RegularExpressions;public class MyClass
    {
    public static void Main()
    {
    String[] arr = {"abcdef","yuybeiusdf","biookkde","aaaaa"};
    Regex r = new Regex("(.)*[b](.)*[e](.)*");
    for(int i=0;i<arr.Length;i ++)
    {
    if(r.IsMatch(arr[i]))
    {
    Console.WriteLine( arr[i] + " 符合要求!");
    }
    else
    {
    Console.WriteLine( arr[i] + " 不符合要求!");
    }
    }
    RL();
    }

    private static void WL(string text, params object[] args)
    {
    Console.WriteLine(text, args);
    }

    private static void RL()
    {
    Console.ReadLine();
    }

    private static void Break() 
    {
    System.Diagnostics.Debugger.Break();
    }
    }
      

  2.   

    直接用string的indexof方法就行了
      

  3.   

    return "abcdef".IndexOf("be") != -1;
      

  4.   

    string s="abcdefg";//or other string like "lkhgebpo","kjieuybbc",etc.
    string patten="[b|e]";
    Regex r=new Regex(pattten);
    Match m=r.Match(s);
    if(m.Success)
    MessageBox.Show("match ok");
      

  5.   

    bool FoundMatch = false;
    try {
    FoundMatch = Regex.IsMatch(SubjectString, "be");
    } catch (ArgumentException ex) {
    // Syntax error in the regular expression
    }
      

  6.   

    lovevsnet(编程一把手) 提供的答案错误,dddbffdsf串也能匹配
      

  7.   

    对不起,没看清题目,上面的不对。重新写一个如下:static bool MatchBE(string input)
    {
      return System.Text.RegularExpressions.Regex.IsMatch(input, "[be]");
    }
      

  8.   

    using System;
    using System.Text.RegularExpressions;class Test
    {
      static bool IsMatchBE(string input)
      {
        return Regex.IsMatch(input, "(b.*e)|(e.*b)");
      }  static void Main()
      {
        string [] ss = { "abcdef", "yuybeiusdf", "biookkde", "dddbffdsf", "eeee" };
        foreach (string s in ss)
        {
          Console.WriteLine("{0,-10}: {1}", s, IsMatchBE(s));
        }
      }
    }/* 程序输出:
    abcdef    : True
    yuybeiusdf: True
    biookkde  : True
    dddbffdsf : False
    eeee      : False
    */