How to find all occurrences of substrings between two characters?

I want to get all substrings between two characters from one long string.
For example, the string can be

This is an example (sentence), and I need to (get) substrings that are (between) parentheses.

and then I need to get all substrings between the parentheses. In the above example, I need to get: sentence, get, and between.
There is no way to know how many parentheses exist in the string.

What is the best way to do this?

If MyText is your longer text, you can get each match by assigning a MatchCollection type variable to System.Text.RegularExpressions.Regex.Matches(MyText, "(?<=\().*(?=\))").

1 Like

i think that dont look very good, wont it get the first ( and the first ) in one big result?
Try this expression: \((.*?)\) and use the first Group to exclude the brackets…

1 Like

You’re right @bcorrea. This one will work: (?<=\()[^\)]*(?=\)).

1 Like

yep, thats a good one, will remove the parenthesis :slight_smile:

1 Like

Thank you both! It worked!

1 Like

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