Loading ...

Regex: text between two characters

Who is online?  0 guests and 0 members
home  »  forums   »  asp.net topics   »  getting started / general asp.net   » Regex: text between two characters

Regex: text between two characters

Posts under the topic: Regex: text between two characters

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 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 inside the bracket (..) 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)