-1

i'm trying to find the first string in a list that starts between E to M (E-M);

startIndexOfMiddleRange = list.IndexOf(list.FirstOrDefault(x => x.Name.StartsWith(())))

what is the pattern or the way i should do that?

thanks for the helpers

ofer gertz
  • 77
  • 1
  • 5
  • Hi Gilad i have 2 variables startIndexOfMiddleRange = list.IndexOf(list.FirstOrDefault(x => x.Name.Length > 0 && x.Name[0] >= 'ח' && x.Name[0] <= 'נ')); endIndexOfMiddleRange = list.IndexOf(list.LastOrDefault(x => x.Name.Length > 0 && x.Name[0] <= 'נ' && x.Name[0] >= 'ח')); i have a sorted list and i want to search in the list the first string that start with a letter between "ח-מ" and the same for the last string – ofer gertz Aug 25 '18 at 09:40

1 Answers1

0

Regex solution:

^[E-M] - This pattern should work.

  • ^ means "starts with"
  • [E-M] limits the range to E-M, and since you're not using any modifiers on it, it will only select a single character.

That being said, using regular expressions for this seems like overkill when you could simply do:

startIndexOfMiddleRange = list.IndexOf(list.FirstOrDefault(x => x.Name.Length > 0 && x.Name[0] >= 'E' && x.Name[0] <= 'M'));

Assuming list is a List<T>, you could simplify it more:

startIndexOfMiddleRange 
    = list.FindIndex(x => x.Name.Length > 0 && x.Name[0] >= 'E' && x.Name[0] <= 'M');
Llama
  • 25,925
  • 5
  • 49
  • 68
  • Hii Jhon thank you for your rapid response. it works for me now. but still, i want to try it with a regulat expression pattern. i didn't find the correct method in the Regex class to do so. do you know how? – ofer gertz Aug 25 '18 at 09:20
  • @ofer [`Regex.IsMatch(x.Name, "^[E-M]")`](https://docs.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex.ismatch?view=netframework-4.7.2) ought to be the method you need. – Llama Aug 25 '18 at 10:29