求助一个正则表达式验证写法

道宣 2014-06-20 08:48:15
请教各位大侠:一个字符串A= "abcde" 另一个字符串 B="ace",想验证 B字符串的的所有字符都存在于A中,而且不一定是顺序或连续的。即: "abcde" 与 "ace" = true
"abcde" 与 "aca" = true
"abcde" 与 "ccc" = true
用正则验证要怎么写啊,没学过这东西,原来是想用折分成字符的方式,但因为要处理的数据量较大,效率不高,所以想試下正則。
...全文
127 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
道宣 2014-06-20
  • 打赏
  • 举报
回复
谢谢各位,确实好用~~
exception92 2014-06-20
  • 打赏
  • 举报
回复
给个更简单的:

            string a = "abcde";
            string b = "affa";
            IEnumerable<char> temp = a.Intersect(b);
            bool isExist =  b.Except(temp).Count() == 0;
gomoku 2014-06-20
  • 打赏
  • 举报
回复
引用 4 楼 jiangsuwx 的回复:
very good!

// even better :)
static bool ContainsAllChars(string str, string chars)
{
    return new HashSet<char>(chars).IsSubsetOf(str);
}
学学你们 2014-06-20
  • 打赏
  • 举报
回复
引用 1 楼 gomoku 的回复:

static bool ContainsAllChars(string str, string chars)
{
    var distinctChars = chars.Distinct().ToList();
    return str.Intersect(distinctChars).Count() == distinctChars.Count;
}
static  void Main(string[] args)
{
    bool b = ContainsAllChars("abcde", "aecc");  // true
}
very good!
md5e 2014-06-20
  • 打赏
  • 举报
回复
[ace]{1,}
bdmh 2014-06-20
  • 打赏
  • 举报
回复
别什么都忘正则那靠,方法很多,正则没准反而麻烦,1楼的方法就不错
gomoku 2014-06-20
  • 打赏
  • 举报
回复

static bool ContainsAllChars(string str, string chars)
{
    var distinctChars = chars.Distinct().ToList();
    return str.Intersect(distinctChars).Count() == distinctChars.Count;
}
static  void Main(string[] args)
{
    bool b = ContainsAllChars("abcde", "aecc");  // true
}

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧