I’m new to regex and was hoping for a pointer towards finding matches for words which are between { } brackets which are words and the first letter is uppercase and the second is lowercase. So I want to ignore any numbers also words which contain numbers
{ test1, Test2, Test, 1213, Tsg12, Tesgd} , test5, test6, {abc, Abc}
so I would only want to bring back matches for:
Test
Tesgd
Abc
I’ve looked at using \b and \w for words that are bound and [Az] for upper followed by lower but not sure how to only get the words which are between the brackets only as well.
Here is your solution:
I assumed it is OK to match inside but not the deepest level paranthesis.
Let’s dissect the regex a bit. AAA means an arbitrary expression. www means an arbitrary identifier (sequence of letters)
.is any character[A-Z]is as you can guess any upper case letter.[^}]is any character but},or}or end-of-string*?means match what came before 0 or more times but lazily (Do a minimal match if possible and spit what you swallowed to make as many matches as possible)(?<=AAA)means AAA should match on the left before you really tryto match something.
(?=AAA)means AAA should match on the rightafter you really match something.
(?<www>AAA)means match AAA and give the string you matched the name www. Only used with ExplicitCapture option.(?<depth>)matches everything but also pushes “depth” on the stack.(?<-depth>)matches everything but also pops “depth” from the stack. Fails if the stack is empty.We use the last two items to ensure that we are inside a paranthesis. It would be much simpler if there were no nested paranthesis or matches occured only in the deepest paranthesis.
The regular expression works on your example and probably has no bugs. However I tend to agree with others, you should not blindly copy what you cannot understand and maintain. Regular expressions are wonderful but only if you are willing to spend effort to learn them.
Edit: I corrected a careless mistake in the regex. (replaced
.*?with[^}]*?in two places. Morale of the story: It’s very easy to introduce bugs in Regex’s.