Click here to Skip to main content
15,889,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi

I have string "First[A,B][true]" this regex can work
C#
^(?<FirstPart>.+)\[(?<SecondPart>.+)]\[(?<ThirdPart>.+)]$


but string can be "First[A,B]".

How can i do it for two string. I mean

for "First[A,B][true]" -> First-A,B-true
for "First[A,B]"-> First-A,B
Posted

1 solution

Easy: make the third group optional, and limit the length of the other two to the minimum possible to get a match.
^(?<FirstPart>.+?)\[(?<SecondPart>.+?)](\[(?<ThirdPart>.+)])?$


[edit]Automatically added HTML closing tags...:grrrr: [/edit]
 
Share this answer
 
v3
Comments
OriginalGriff 28-Feb-15 12:15pm    
Oi! Piebald! put my regex back, I was just about to fix the auto HTML! :laugh:
PIEBALDconsult 28-Feb-15 12:16pm    
It was there in the preview. I can't find it either, and we can't access version? Sorry.
This is why I try to remember to always Ctrl-A / Ctrl-C before hitting submit. (I didn't this time.)
OriginalGriff 28-Feb-15 12:18pm    
No problem - the original was still there in the Expresso editing pane!
PIEBALDconsult 28-Feb-15 12:23pm    
Phew!

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