I'm trying to find a RegEx pattern that lets me match on all parentheses (and their content) as long as these parentheses are between {
and }
.
Examples:
{foo (i,j) bar}
should match on (i,j)
{(i,j) foo (k,l) bar (m,n,o)}
should match on (i,j)
, (k,l)
, and (m,n,o)
.foo (i,j) bar
should not match on anything because the string is not between swirly brackets.{foo (i,j) bar} (k,l)
should match on (i,j)
but not (k,l)
because the latter is outside of the swirly brackets.The closest I came was with this pattern: (?<=\{)[^\(].*\(.*?\).*(?=\})
. This pattern matched on the first, second, and fourth example, but matched on all of the content between the swirly brackets instead of only the parentheses and their content.
To match literal curly braces, you have to escape them with \ . However, Apex Code uses \ as an escape, too, so you have to "escape the escape". You'll need to do this almost every time you want to use any sort of special characters in your regexp literally, which will happen more frequently than not.
The curly brackets are used to match exactly n instances of the proceeding character or pattern. For example, "/x{2}/" matches "xx".
Use Parentheses for Grouping and Capturing. By placing part of a regular expression inside round brackets or parentheses, you can group that part of the regular expression together. This allows you to apply a quantifier to the entire group or to restrict alternation to part of the regex.
Square brackets match something that you kind of don't know about a string you're looking for. If you are searching for a name in a string but you're not sure of the exact name you could use instead of that letter a square bracket. Everything you put inside these brackets are alternatives in place of one character.
The word “brackets” can mean two different things. Firstly, it is an umbrella term that can refer to any of the four different kinds of brackets. Secondly, it can be used as the name for one specific kind of bracket, the square brackets. Parentheses, on the other hand, are a different thing altogether: round brackets.
The parentheses let you extract the matched portion. substring (1 means to start one character in (just past the first {) and ,g.length-1) means to take characters until (but not including) the character at the string length minus one. This works because the position is zero-based, i.e. g.length-1 is the last position.
Print all combinations of balanced parentheses. If open bracket count becomes more than the close bracket count, then put a closing bracket and recursively call for the remaining brackets. If open bracket count is less than n, then put an opening bracket and call _printParenthesis () for the remaining brackets.
The singular term is a parenthesis, and the plural signifies the pair of round brackets. Parentheses are used for afterthoughts and to add additional information to a sentence (like so). This information or afterthought is known as a “parenthesis” and should always be enclosed in brackets, dashes, or commas.
You can use
(?:\G(?!\A)|{)[^{}]*?\K\([^()]*\)
See the regex demo. If you want to make absolutely sure there is a closing }
on the right, add a (?=[^{}]*})
positive lookahead at the end:
(?:\G(?!\A)|{)[^{}]*?\K\([^()]*\)(?=[^{}]*})
See this regex demo.
Details
(?:\G(?!\A)|{)
- either end of the previous successful match or a {
char[^{}]*?
- zero or more chars other than {
and }
, as few as possible\K
- match reset operator that discards all text matched so far from the current overall match memory buffer\(
- a (
char[^()]*
- zero or more chars other than (
and )
as many as possible\)
- a )
char(?=[^{}]*})
- immediately on the right, there must be zero or more chars other than {
and }
and then a }
.See an R demo online:
x <- "{(i,j) foo (k,l) bar (m,n,o)} should match on (h,j), (a,s), and (i,o,g)."
regmatches(x, gregexpr("(?:\\G(?!\\A)|{)[^{}]*?\\K\\([^()]*\\)(?=[^{}]*})", x, perl=TRUE))
# [[1]]
# [1] "(i,j)" "(k,l)" "(m,n,o)"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With