Click here to Skip to main content
15,745,574 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

I have the following code to identify one or more groups of text inside of parentheses (along with the parentheses):

I've tried to add a Positive Lookbehind Assertion so it would only identify text enclosed in parentheses when preceded by specific pattern:
(?<=NA - FY\d\d [A-Z][A-Za-z][A-Z]|(?<=NA - FY\d\d [A-Z][A-Z][A-Z][A-Z])) \((?!([^)]*\b(ARR)\b[^(]*)).*?\)

Here is some sample folder names I am using to test the Expression:

NA - FY17 JCIG (Joint Coord Integration Grp) briefs
NA - FY17 JIPT (Joint Integr Product Tm) briefs
NA - FY17 SoW (Statement of Work)
NA - FY17 SRRB (Serv Requi Review Brd) Quad chart
NA - FY17 UFR (Unfunded Plan) - CYD (Current Year)
NA - FY17 DR (Directors Reviews) briefs
NA - FY17 PP (Phasing Plans)
NA - FY17 Funding Docs
MVSOT (Maritime Vessel)
It's doing what I want by not capturing the parentheses in the last 4 lines of text. Problem is it's not capturing both groups of text enclosed in parentheses in the line above them. In that line of text
NA - FY17 UFR (Unfunded Plan) - CYD (Current Year)
it no longer captures the 2nd group of text in parenthesis. I was given the Regex that captures the text in parentheses so I'm not entirely sure how it works but I'm pretty sure the Positive Lookbehind Assertion is correct. Can anyone please tell me what I am missing?

What I have tried:

Have tried negative look behind ((?
Updated 15-Oct-19 7:28am
[no name] 15-Oct-19 11:06am    
Does it have to be Regex? Why not use Linq instead?
Member 14623559 15-Oct-19 13:44pm    
Thank you. Not familiar with Linq. Know it can be done as Regex and that's what I'm familiar with.
[no name] 15-Oct-19 14:00pm    
Would you care for a Linq solution instead? Regex is costly when it comes to performance, so it's advised to keep it to a minimum.

See this: RegExr: parentheses[^]
So that would be:
Share this answer
Member 14623559 15-Oct-19 13:48pm    
No, it takes off the one not following the pattern (MVSOT (Maritime Vessel)) which I don't want it to do.
RickZeeland 15-Oct-19 14:00pm    
No idea what that means, sorry ! As far as I can see everything between parentheses is parsed correctly.
Member 14623559 15-Oct-19 14:54pm    
The Positive Lookbehind Assertions, (?<=NA - FY\d\d [A-Z][A-Za-z][A-Z]|(?<=NA - FY\d\d [A-Z][A-Z][A-Z][A-Z])), should capture text in parenthesis ONLY when followed by "NA - FY \d\d". I have a link to regex101 in my original question that could help. If not thank you for the help anyways!
RickZeeland 15-Oct-19 16:12pm    
Something like this would be easier to implement in C# or other language I think, maybe there is a way to do it in Regex but that will take a lot of time to find out ...
Member 14623559 16-Oct-19 6:47am    
Ok gotcha, makes sence, thank you for the insight.
Just a few interesting links to help building and debugging RegEx.
Here is a link to RegEx documentation:
perlre -[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
RegExr: Learn, Build, & Test RegEx[^]
Online regex tester and debugger: PHP, PCRE, Python, Golang and JavaScript[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx: Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
This site also show the Regex in a nice graph but can't test what match the RegEx: Regexper[^]
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900