RegEx to find a word not enclosed in paranthesis

  • Thread starter Thread starter vmoreau
  • Start date Start date
V

vmoreau

I have a text and I need to find a Word that are not enclosed in
paranthesis. Can it be done with a regex? Is someone could help me?
I am not familar with regex...

Example looking for WORD:
(there is a WORD in ( my string WORD )) and * WORD * to (find WORD)
and * WORD *

Should give me the to word between star (star ar not part of string)

thanks a lot
 
I have a text and I need to find a Word that are not enclosed in
paranthesis.

Did you try character classes. This'll givu an idea to get started:

[^(]WORD[^)]

~Jason

--
 
I have a text and I need to find a Word that are not enclosed in
paranthesis. Can it be done with a regex? Is someone could help me?
I am not familar with regex...

Example looking for WORD:
(there is a WORD in ( my string WORD )) and * WORD * to (find WORD)
and * WORD *

Should give me the to word between star (star ar not part of string)

thanks a lot
 
I like this method :)

StringBuilder sb = new StringBuilder();
int depth = 0;
foreach (char c in str)
{
if (c == '(') depth++;
else if (c == ')') depth--;
else if (depth == 0) sb.Append(c);
}

string noParentheses = sb.ToString();
 
Chris' method is best, since regexes don't deal well with recursive
concepts. After all, you don't want to just consider the case of "am I
in parentheses" - you have to be able to handle that in
((((( foo )))) bar (((( baz )))))
bar is within the outermost set of parens.
 
Back
Top