2

There is a list which adds the string at runtime. The string in the list can be like

List<string> myList=new List<string>();
  0    7- 9
  1    3 - 6
  2    1 -3
  3    10-12

Here the string contain in list does not have same pattern. Suppose I want to find the index of 3 - 6. So I have use an expression

3\s*\-\s*6

Now how to use it in Array.Indexof method so that I can get index of this element from mylist.

2
  • List<string> myList = new List(); should be List<string> myList = new List<string>(); shouldn't it? And why Regex? Why not simply use .Find() or .Contains()? Commented Sep 12, 2013 at 7:43
  • .contains will not work because in advance I dont know what would be the pattern of string in a list. Commented Sep 12, 2013 at 8:51

4 Answers 4

2

Try

myList.FindIndex(s => new Regex(@"3\s*\-\s*6").Match(s).Success);

Edit: Working sample:

        List<string> myList = new List<string>
            {
                "7- 9",
                "3 - 6",
                "1 -3",
                "10-12"
            };
        int index = myList.FindIndex(s => new Regex(@"3\s*\-\s*6").Match(s).Success);


        Console.WriteLine(index); // 1
Sign up to request clarification or add additional context in comments.

5 Comments

its giving error as Expression cannot contain lambda expressions
Hmm is that a compile-time error? Can you tell me more about the error?
Also, forgot to put a @ before the regular expression, ive edited my answer.
which line is throwing that error? the problem must be somewhere else in your code. I've tested this, and it works. Ive edited my answer and added a working sample.
I believe its QuickWatch showing that error, the code is fine
0

you can do

str.Replace(" ", "");

and then you get rid of whitespace

and can just do

str.IndexOf("3-6");

3 Comments

Calling replace on every element might imply creating a whole new list of strings... and then doing a search.
@dcastro unless you insert it that way to the list. plus you don't have to create a new list. doing str.Replace(" ", "").IndexOf("3-6"); is possible...
Correct me if I'm wrong, but I think he's searching for a string in a list, not a substring in a string (which is what str.Replace().IndexOf() does)...
0

Try this :

var match = Regex.Match(String.Join(String.Empty, myList.ToArray()), @"3\s*\-\s*6");

if (match.Success) {
    // match.Index to get the index
    // match.Value to get the value
}

1 Comment

@Bidhu I have list and not yourstring
0

You can do the same using LINQ:

var regex = new Regex(@"3\s*\-\s*6");
var index = myList.Select((x, i) => new { x, i })
                  .Where(x => regex.Match(x.x).Success)
                  .Select(x => x.i)
                  .First()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.