How to get matched string from Regex? [C#]

.net, c#, regex, winforms

Solution

Use Regex.IsMatch method to check if regular expression finds a match in the input string. E.g

    foreach (var item in selectedItems)
    {
         if (filter.IsMatch(item.ToString())
             // matched
    }

BTW Keep in mind that `[0-9]*` will match anything, because you don't require any numbers to be in input string. Possibly you need `^\d+$`

UPDATE: Getting matched number:

Regex filter = new Regex(@"(\d+)");

foreach (var item in checkedListBox1.CheckedItems)
{
    var match = filter.Match(item.ToString());
    if (match.Success)
    {
        MessageBox.Show(match.Value);
    }    
}

Problem

I have the following code: ``` private void button_borrow_Click(object sender, EventArgs e) { Regex filter = new Regex(@"[0-9]*"); String items = ""; var selectedItems = checkedListBox_bookview.CheckedItems; foreach (var item in selectedItems) { } MessageBox.Show(items.ToString() + " Were selected: " + selectedItems.Count); } ``` I want to get the matched Strings from `filter`. how do I do so?

Original source