Loading ...

Regex help

Who is online?  0 guests and 0 members
home  »  forums   »  asp.net topics   »  getting started / general asp.net   » Regex help

Regex help

Posts under the topic: Regex help

Posted: 9/11/2010

Lurker 100  points  Lurker
  • Joined on: 9/11/2010
  • Posts: 20

Hi friends, 

I need some help regarding regex. I have a long string which contains words like this "I am (peter) smith I (want) to make a (regex)"

Now from this string I want to extract all those words which are in this format (..) i.e I need the output to be (peter),(want),(regex)

Thanks,

Peter

 


tags Regex

Posted: 9/11/2010

Guru 16813  points  Guru
  • Joined on: 4/19/2009
  • Posts: 490
  Answered

 

#region Using Directives

using System;
using System.Text.RegularExpressions;

#endregion

public class Example
{
    static void Main()
    {
        var regex = new Regex(@"(\(.*?\))",
                              RegexOptions.Singleline | RegexOptions.IgnoreCase);

        const string input = "I am (peter) smith I (want) to make a (regex)";
        var matches = regex.Matches(input);
        foreach (var match in matches)
        {
            Console.WriteLine(match.ToString());
        }
        Console.ReadLine();
    }
}


Output:

 

 

(peter)

(want)

(regex)

 


tags regex
Page 1 of 1 (2 items)