-1

Trying to match string with multiple parenthesis in the key(value) pattern.

String 1:

hostportservice(192.168.1.241(10001), service(master))   hostportservice(192.168.1.200(10001), service(slave))

String 1 Matches

hostportservice(192.168.1.241(10001), service(master))
hostportservice(192.168.1.200(10001), service(slave))

String 2:

hostportservice(192.168.1.241(10001), service(master))   updatedate(24-DEC-2015) updatetime(11:32:57 PM)

String 2 Matches

hostportservice(192.168.1.241(10001), service(master))
updatedate(24-DEC-2015)
updatetime(11:32:57 PM)
marc_s
  • 675,133
  • 158
  • 1,253
  • 1,388
  • I'm preparing a solution, but can you please define clearly, what is the input and what is the desired output? For example problem statement, please check this question: http://stackoverflow.com/questions/33181434/regex-matching-any-character-which-repeats-n-times – ferit Dec 25 '15 at 10:35
  • Input string may be a multi line string, each string in a line may contain multiple strings separated by white space(s). The match would be in this pattern key(value). But the value may have more pairs of parentheis or spaces in it. I'm finding this part much challenging. Thanks for atleast responding in a positive way. – Vinay Sathyanarayana Dec 25 '15 at 10:39
  • No no, it's not the proper way, just give me the input text, and output text. Give me something that I can test my trials on it. Explaining it in English is not clear. – ferit Dec 25 '15 at 10:41
  • Updated the question, please check and let me know if it is much clear now. Thanks – Vinay Sathyanarayana Dec 25 '15 at 10:48
  • Yes, it's clear now. I found two different regexes matching for two different inputs but I'm trying to merge them together, which is a bit tricky. – ferit Dec 25 '15 at 10:57
  • Thats where i'm stuck. I tried many options but i'm not able to match the required output in the second string. – Vinay Sathyanarayana Dec 25 '15 at 10:59
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/98970/discussion-between-vinay-sathyanarayana-and-saibot). – Vinay Sathyanarayana Dec 25 '15 at 11:08

3 Answers3

1

I don't think RegEx is the right approach for this problem, which would be easier solved programmaticaly. In C# this means iterating through the string, counting parantheses and splitting each time the paranthesis count is 0 and you hit a space.

vauhochzett
  • 1,035
  • 11
  • 25
1

A bit complex:

([^\s]+\s[^\s]+)\s+([^\s]+)\s+([^\s]+\s[^\s]+)|([^\s]+\s[^\s]+)\s+([^\s]+\s[^\s]+)

Demo here: https://regex101.com/r/jP9uO0/1

ferit
  • 4,937
  • 3
  • 27
  • 49
  • I sincerely thank you for your time. However, i tried with a different string but matches only a couple of the complete string. Link here https://regex101.com/r/yT5lI9/1 – Vinay Sathyanarayana Dec 25 '15 at 11:05
  • Sorry, problem is not my solution. Problem is your question. Your test case in this link is TOTALLY different from the set you gave me. First, you have to properly decide what the problem is, and then we can solve it. But what is actually happening here is we are chasing our tails... Please check this question to learn how to state a regex problem clearly: http://stackoverflow.com/questions/33181434/regex-matching-any-character-which-repeats-n-times?lq=1 – ferit Dec 25 '15 at 11:09
  • Thanks. I agree its not a problem with your solution. I gave a sample pattern / string. No matter the length of the string, the pattern to be extracted remains the same in the example i gave. Thank you very much. – Vinay Sathyanarayana Dec 25 '15 at 11:13
0

What you are trying to achieve is done using Balancing groups in .NET Regex. You should read-up on the below links

Below is the code that works for your case. You might need to play with it to make it work for all your cases.

var pattern = new Regex(
                    @"\s*(?'TXT'(?:" +               /* Let's capture this expression */
                        @"[^()]*" +                  /* Part before parens start */
                        @"(?:(?'OPEN'\()[^()]*)+" +  /* Capture open paren followed by any text */
                        @"(?'-OPEN'\))+" +           /* Now remove the captured open paren group for every closed paren */
                    @")+?)" +                        /* Finished capture group */
                    @"(?(OPEN)(?!))"                 /* Confirm that no extra open parens are left on the stack */
                    );
var inputString = 
        @"hostportservice(192.168.1.241(10001), service(master))   hostportservice(192.168.1.200(10001), service(slave))" + Environment.NewLine +
        @"hostportservice(192.168.1.241(10001), service(master))   updatedate(24-DEC-2015) updatetime(11:32:57 PM)";

Match match;
int startAt = 0;
while ((match = pattern.Match(inputString, startAt)).Success) {
    Debug.WriteLine("Pattern Matched: " + match.Groups["TXT"].Value);
    startAt = match.Index + match.Length;
}
Community
  • 1
  • 1
Vikhram
  • 3,974
  • 1
  • 15
  • 30