I am trying to write a program that takes a regular string with quotations and nested brackets and breaks them down in a list.
So far I am using this RegEX: @"[\""].+?[\""]|\{.*\}|(?=\()(?:(?=.*?\((?.?*.\1)(?*\)(..?*\2).*))(?=?*.\)(?..*?\2)(.*))?)+??*?(?=\1)[^(]*(?=\2$)|[^ ]+"
What i want it to do is this: if (eval (date day) == "14") {print "Today is the 14th"} else {print "It is not the 14th"}
if
(eval (date day) == "14")
{print "Today is the 14th"}
else
{print "It is not the 14th"}
but it returns as
if
(eval (date day) == "14")
{print "Today is the 14th"} else {print "It is not the 14th"}
I had this problem with the parenthesis and found a solution online but when I tried to change it to work with the {} it doesn't work.
I read online that RegEX doesn't work but I have not found a new solution. Is there any way I can do this?
If the delimiters are () and {} and you want to ignore string content that could contain
delimiters you just need to use a balanced text regex.
(?:[^(){}]+|(?:(?:(?'opP'\()(?>[^()"]+|"[^"]*")*)+(?:(?'clP-opP'\))(?>[^()"]+|"[^"]*")*?)+)+(?(opP)(?!))|(?:(?:(?'opBr'\{)(?>[^{}"]+|"[^"]*")*)+(?:(?'clBr-opBr'\})(?>[^{}"]+|"[^"]*")*?)+)+(?(opBr)(?!)))
C# sample
Regex RxParts = new Regex(@"(?:[^(){}]+|(?:(?:(?'opP'\()(?>[^()""]+|""[^""]*"")*)+(?:(?'clP-opP'\))(?>[^()""]+|""[^""]*"")*?)+)+(?(opP)(?!))|(?:(?:(?'opBr'\{)(?>[^{}""]+|""[^""]*"")*)+(?:(?'clBr-opBr'\})(?>[^{}""]+|""[^""]*"")*?)+)+(?(opBr)(?!)))" );
string test_sample = @"if (eval (date day) == ""14"") {print ""Today is the 14th""} else {print ""It is not the 14th""}";
Match M = RxParts.Match(test_sample);
while ( M.Success )
{
string strM = M.Value.Trim();
if ( strM.Length > 0 )
Console.WriteLine("{0}", strM);
M = M.NextMatch();
}
output
if
(eval (date day) == "14")
{print "Today is the 14th"}
else
{print "It is not the 14th"}
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.