正则提取 IMEI 信息和 动态构建 Lambda 表达式


完整代码

public class User
{
    public int? IsDeleted;
    public string Name;
}

class Program
{
    static void Main(string[] args)
    {

        ParameterExpression pe = Expression.Parameter(typeof(User), "user");
        var exp = Expression.Equal(Expression.PropertyOrField(pe, "IsDeleted"), Expression.Constant(0, typeof(int?)));
        var lambda = Expression.Lambda<Func<User, bool>>(exp, pe);
        List<User> users = new List<User>();
        users.Add(new User { Name = "1", IsDeleted = 0 });
        users.Add(new User { Name = "2", IsDeleted = 1 });
        users.Add(new User { Name = "3", IsDeleted = 1 });
        users.Add(new User { Name = "4", IsDeleted = 0 });

        Console.WriteLine(exp.ToString());
        Console.WriteLine(users.AsQueryable().Count(lambda));

        Regex reg1 = new Regex(@"(?<imeis>8670\d{11})");

        List<Tuple<string, string, string, string>> list = new List<Tuple<string, string, string, string>>();

        using (StreamReader sr = new StreamReader(@"E:\Changweihua\NewMan.txt", Encoding.UTF8))
        {
            String line;
            while ((line = sr.ReadLine()) != null)
            {
                string imei1 = "";
                string imei2 = "";
                var matchs = reg1.Matches(line);
                if (matchs.Count == 1)
                {
                    imei1 = matchs[0].Value;
                }
                if (matchs.Count == 2)
                {
                    imei1 = matchs[0].Value;
                    imei2 = matchs[1].Value;
                }

                list.Add(new Tuple<string, string, string, string>("纽扣用户", "确认参与5.0尝鲜", imei1, imei2));
            }
        }
        string savePath = @"E:\Changweihua\NewMan.csv";
        //if (File.Exists(savePath))
        //{
        //    File.Delete(savePath);
        //}
        //File.Create(savePath);
        using (StreamWriter sw = new StreamWriter(savePath, true, Encoding.UTF8))
        {
            foreach (var tuple in list)
            {
                sw.WriteLine(string.Format("{0},{1},'{2},'{3}", tuple.Item1, tuple.Item2, tuple.Item3, tuple.Item4));
            }
        }
        Console.WriteLine("----------------------");
        Console.ReadKey(true);
    }
}

知识共享许可协议
《正则提取 IMEI 信息和 动态构建 Lambda 表达式》 常伟华 创作。
本作品采用知识共享署名-相同方式共享 4.0 国际许可协议 | 3.0 中国大陆许可协议进行许可。

站内公告