How to include second square brackets in my match result. If I have regex like
\\[msg.(?<msgfield>.*?)\\]
My input string is
[url]/XYZ/[SomeClass.Entity["Id"]]
Then how to get the result match of Entity["Id"]
.
Here is what I tried so far. I am missing the last ]
. The result of my code is Entity["Id"
.
Also, the input string could be [url]/XYZ/[SomeClass.EntityId]
. It should give result of EntityId
then.
using System;
using System.Text.RegularExpressions;
public class Program
{
public static void Main()
{
String sample = "[url]/XYZ/[SomeClass.Entity[\"Id\"]]";
Regex regex = new Regex(@"\[SomeClass.(?<msgfield>.*?)\]");
Match match = regex.Match(sample);
if (match.Success)
{
Console.WriteLine(match.Groups["msgfield"].Value);
}
}
}
\[SomeClass\.(?<msgfield>[^]]+\]?)\]
A few things:
.*?
inside the capturing group.
character to make it a literal match instead]
inside the capturing group (if present)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.