Linq / Regex to find values which are outside brackets

Hi I have a sentence as follows.
I should retrieve the substring whic is outside the circular brackets.

Can someone help please?

Exampl 1: (Roses) are (Red Milk is) white

should return-> are and white seperately in an array.
arr[0] = are
arr[1] = white

Example 2:roses) are red milk (is white)
result

arr[0] = are red milk

Hi @JITU99 - Try the below exp

(?<=\(.*\)).*(?=\(.*\))|(?<=\(.*\).*\(.*\)).*

grafik

Assign Activity
strPattern = (?<=\)).*?(?=\(|$)

Assign Activity
arrValues | String Array =

System.Text.RegularExpressions.RegEx.Matches(YourStringVar, strPattern).Cast(Of Match).Select(Function (x) x.Value.Trim).toArray

First Value → arrValues(0)
Second Value → arrValues(1)

Thanks @ppr for the solution but for some cases its not working examples below.With outputs

(successful) football (club(s)) (managers)" ----> {“football”,“)”,“”}

“boat(s)” ----> empty array

“abcd (asdf) boat(s)” ----> {“boat”,“”}

better that you will write down all variations (input, output), so we can check for a new pattern

For regex is not recommended to start with half-defined requirements and then run it against another case. Regex is not about that a pattern can support every case. Also the strategy to setup an extraction over a regex extraction cascade / regex / string methods cascade can help as well

Hi @ppr Most of my combinations are these but there can be chances the sentence is more complex or bigger one with the brackets.

Are these combinations enough?

(successful) football (club(s)) (managers) volleyball Cricket (New(s))
(a) farmer(s)
(the) farmer(s)
(a) teacher(s)
(the) teacher(s)
(successful) football (club(s)) (managers)
(successful) soccer (club(s)) (managers)
(a) (new) magazine(s) (for teenagers)
(the) (new) magazine(s) (for teenagers)
(a) boat(s)
(the) boat(s)
Tuesday(s)
abcd (asdf) boat(s)
boat(s)
(successful) football (club(s)) (managers)

(successful) football clubs (managers)

Abcd (efgh) hgji asdf (xyz) qwerty ------> hgji asdf should be one out put instead of hdji and asdf being sepearetly in the array of string

can you just mark in bold / or list down what output is needed? Thanks

i just edited my question

Maybe a replacement strategy is better fitting

We replace the brackets with a # and then split / extract / trim the remaining parts

That will work but can i get the query for it please?

 strText
 "(successful) football (club(s)) (managers) volleyball Cricket (New(s))"
 strPattern
 "\(.+?\)?\)"
 System.Text.RegularExpressions.Regex.Replace(strText,strPattern,"#").Split({"#"}, StringSplitOptions.RemoveEmptyEntries)
 string[3] { " football ", " ", " volleyball Cricket " }
 arrValues = System.Text.RegularExpressions.Regex.Replace(strText,strPattern,"#").Split({"#"}, StringSplitOptions.RemoveEmptyEntries)
 string[3] { " football ", " ", " volleyball Cricket " }
 arrValues = arrValues.Select(Function (x) x.Trim() ).Where(Function (x) Not String.IsNullOrEmpty(x)).toArray
 string[2] { "football", "volleyball Cricket" }

Thank You this helped.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.