1

Please help how to combine the below two regular expressions

a) @"(?<=\.Value\[)(.*)(?=\])"

Above regular expression takes out a substring after .Value and it truncates the '[' and ']' brackets from the substring.

b) @"[^\{*\}*$]+"

Removes all the '{' and '}' braces from the beginning and end of the above substring. The substring can have zero or more number of occurrences of '{' and '}'characters.

E.g.

{{server/Y2KCluster1:[]ServerServerStatusState}}.Value[{{{server/Y2KCluster2:[]ServerServerStatusSecondsTillShutdown}}}]

After applying 1st regular expression it returns as

[{{{server/Y2KCluster2:[]ServerServerStatusSecondsTillShutdown}}}]

After applying 2nd regular expression then it return as

server/Y2KCluster2:[]ServerServerStatusSecondsTillShutdown

slugster
  • 49,403
  • 14
  • 95
  • 145
  • 2
    Please provide some sample inputs you are using and what you would be expecting. – npinti Mar 21 '16 at 06:42
  • Maybe this SO [post](http://stackoverflow.com/questions/2145651/combining-these-two-regular-expressions-into-one) will help – Mihail Stancescu Mar 21 '16 at 06:49
  • {{server/Y2KCluster1:[]ServerServerStatusState}}.Value[{{{server/Y2KCluster2:[]ServerServerStatusSecondsTillShutdown}}}] After applying first regular expression then it return as [{{{server/Y2KCluster2:[]ServerServerStatusSecondsTillShutdown}}}] After applying 2nd regular expression then it return as server/Y2KCluster2:[]ServerServerStatusSecondsTillShutdown – Shibu Veerappallil Mar 21 '16 at 06:52
  • Edit your question @ShibuVeerappallil, instead of posting as comment – Garfield Mar 21 '16 at 06:56
  • Thanks. I updated the question. – Shibu Veerappallil Mar 21 '16 at 07:02

2 Answers2

2

The following regular expression

(?<=\.Value\[)(?:{*)([^}]+)(?=[\]}])

will

  • find the substring after .Value[
  • and match any number of { characters greedily
  • greedily match everything before a } character - returned in group 1
  • end before a } or ] character

demo: https://regex101.com/r/mA7pH2/1

Keith Hall
  • 15,362
  • 3
  • 53
  • 71
  • Thanks for your reply. I tried your solution and it return as `code` {{{{server/S2KCluster2:[]ServerServerStatusSecondsTillShutdown}. – Shibu Veerappallil Mar 21 '16 at 07:52
  • @Shibu look at the [table tab at regexstorm](http://regexstorm.net/tester?p=(%3f%3c%3d%5c.Value%5c%5b)(%3f%3a%7b*)(%5b%5e%7d%5d%2b)(%3f%3d%5b%5c%5d%7d%5d)&i=%7b%7bserver%2fY2KCluster1%3a%5b%5dServerServerStatusState%7d%7d.Value%5b%7b%7b%7bserver%2fY2KCluster2%3a%5b%5dServerServerStatusSecondsTillShutdown%7d%7d%7d%5d) and see that the result you desire is correctly captured in $1 aka group 1, as I mentioned in my answer and as is shown on regex101 – Keith Hall Mar 21 '16 at 07:57
  • Thanks its working now. Previously i didn't extract the value $1. Now i extracted the $1 value as .Groups[1].value. Thanks for your answer. – Shibu Veerappallil Mar 21 '16 at 08:06
  • Thanks Keith Hall. – Shibu Veerappallil Mar 21 '16 at 08:23
0

This regex:

\.Value\[\{*((?:\[[^\]]*\]|.)*?)}*\]

looks for a match to the Value property. Then matches a [ followed by any number of {. Then, and this is where the work is done, in a capture group - get any of

  • \[[^\]]*\] - a [ followed by anything up to a ] (matching brackets - in your example simply [])

  • . any character

repeat this non-greedy until any number of } followed by a ].

The result is captured in group 1.

Regards

SamWhan
  • 8,296
  • 1
  • 18
  • 45