Miller
Miller

Reputation: 1156

Matching any word enclosed in parentheses in a sentence

I am trying to find a regex to match any word enclosed in parentheses in a sentence.

Suppose, I have a sentence.

"Welcome, (Hello, All of you) to the Stack Over flow."

Say if my matching word is Hello,, All, of or you. It should return true. Word could contain anything number , symbol but separated from other by white-space

I tried with this \(([^)]*)\). but this returns all words enclosed by parentheses

static void Main(string[] args)
        {

            string ss = "Welcome, (Hello, All of you) to the Stack Over flow.";
            Regex _regex = new Regex(@"\(([^)]*)\)");
            Match match = _regex.Match(ss.ToLower());
            if (match.Success)
            {
                ss = match.Groups[0].Value;
            }

        }

Help and Guidance is very much appreciated.

Thanks.

Thanks People for you time and answers. I have finally solved by changing my code as reply by Tim. For People with similar problem. I am writing my final code here

static void Main(string[] args)
        {
            string ss = "Welcome, (Hello, All of you) to the Stack Over flow.";
            Regex _regex = new Regex(@"[^\s()]+(?=[^()]*\))");
            Match match = _regex.Match(ss.ToLower());
            while (match.Success)
            {
                ss = match.Groups[0].Value;
                Console.WriteLine(ss);
                match = match.NextMatch();
            }

        }

Upvotes: 0

Views: 1934

Answers (3)

Amit Joki
Amit Joki

Reputation: 59232

I've developed a c# function for you, if you are interested.

public static class WordsHelper
{
    public static List<string> GetWordsInsideParenthesis(string s)
    {
        List<int> StartIndices = new List<int>();
        var rtn = new List<string>();
        var numOfOpen = s.Where(m => m == '(').ToList().Count;
        var numOfClose = s.Where(m => m == ')').ToList().Count;
        if (numOfClose == numOfOpen)
        {
            for (int i = 0; i < numOfOpen; i++)
            {
                int ss = 0, sss = 0;
                if (StartIndices.Count == 0)
                {
                    ss = s.IndexOf('(') + 1; StartIndices.Add(ss);
                    sss = s.IndexOf(')');
                }
                else
                {
                    ss = s.IndexOf('(', StartIndices.Last()) + 1;
                    sss = s.IndexOf(')', ss);
                }
                var words = s.Substring(ss, sss - ss).Split(' ');
                foreach (string ssss in words)
                {
                    rtn.Add(ssss);
                }
            }
        }
        return rtn;
    }
}

Just call it this way:

var text = "Welcome, (Hello, All of you) to the (Stack Over flow).";
            var words = WordsHelper.GetWordsInsideParenthesis(s);

Now you'll have a list of words in words variable.

Generally, you should opt for c# coding, rather than regex because c# is far more efficient and readable and better than regex in performance wise.

But, if you want to stick on to Regex, then its ok, do the following:

If you want to use regex, keep the regex from Tim Pietzcker [^\s()]+(?=[^()]*\)) but use it this way:

var text="Welcome, (Hello, All of you) to the (Stack Over flow).";
var values= Regex.Matches(text,@"[^\s()]+(?=[^()]*\))");

now values contains MatchCollection

You can access the value using index and Value property

Something like this:

string word=values[0].Value;

Upvotes: 1

Srb1313711
Srb1313711

Reputation: 2047

(?<=[(])[^)]+(?=[)])

Matches all words in parentheses

(?<=[(]) Checks for (

[^)]+ Matches everything up to but not including a )

(?=[)]) Checks for )

Upvotes: 0

Tim Pietzcker
Tim Pietzcker

Reputation: 336128

OK, so it seems that a "word" is anything that's not whitespace and doesn't contain parentheses, and that you want to match a word if the next parenthesis character that follows is a closing parenthesis.

So you can use

[^\s()]+(?=[^()]*\))

Explanation:

  • [^\s()]+ matches a "word" (should be easy to understand), and
  • (?=[^()]*\)) makes sure that a closing parenthesis follows:

    (?=       # Look ahead to make sure the following regex matches here:
     [^()]*   # Any number of characters except parentheses
     \)       # followed by a closing parenthesis.
    )         # (End of lookahead assertion)
    

Upvotes: 1

Related Questions