I've a class
public class MyClass
{
public MyClass(int id, string name)
{
this.Id = id;
//Id = id;
this.Name = name;
}
public int Id { get; set; }
public string Name { get; set; }
}
I created a list of MyClass type and added few items to it
List<MyClass> lst = new List<MyClass>();
lst.Add(new MyClass(1, "Name1"));
lst.Add(new MyClass(2, "ab****cdefg"));
lst.Add(new MyClass(3, "he*llo"));
lst.Add(new MyClass(4, "pa**yed"));
lst.Add(new MyClass(5, "Names2"));
lst.Add(new MyClass(6, "hi******iiii"));
lst.Add(new MyClass(7, "so*me"));
lst.Add(new MyClass(8, "so**rt"));
lst.Add(new MyClass(9, "*"));
lst.Add(new MyClass(10, "**"));
lst.Add(new MyClass(11, "t*e*s*t"));
lst.Add(new MyClass(12, "t**e**s**t"));
How can i find items having only one * in name field in a list and two ** in name field in another list. I'ld like to do it by linq or regex, any other good option is also invited. I tried using containsmethod but got all items having *.
var singlestar = lst.Where(x => x.Name.Contains("*")).ToList();
Samuel Fernandes
27-Jan-2015this should match names with just one "\*" and two "\*\*"
var singlestar = lst.Where(x =>Regex.Matches(x.Name, "\\*").Count == 1).ToList();
var twostar = lst.Where(x =>Regex.Matches(x.Name, "\\*\\*").Count == 1).ToList();