using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = @"(?(DEFINE)
(?<EXPR>
(?&EA) (?&EB)
)
(?<EA>
\( (?&EXPR) \) |
(?&ANY) |
(?&PREFIX) (?&EXPR)
)
(?<EB> # EXPR beta
(?&INFIX) (?&EXPR) |
(?&POSTFIX) |
)
# Operators
(?<INFIX>[+*\/-^])
(?<PREFIX>[+-])
(?<POSTFIX>)
# Literals
(?<ANY>
(?&INT)|(?&STRING)|(?&BOOL)|
(?&PROP)|(?&FUNC)
)
(?<FUNC>
=>\s*\(
((?&EXPR)(,\s*(?&EXPR))*)?
\)\s*
({(?&EXPR)}|(?&EXPR))
)
(?<PROP>
(?<PALPHA>
([A-Za-z$_][\w$]*)(\(
((?&EXPR)(,\s*(?&EXPR))*)?
\))?
)
(\.(?&PALPHA))*
)
(?<BOOL>true|false)
(?<STRING>
([""'])([^""\\]|\\.)*?\g{-2}
)
(?<INT>\d+)
)
^(?&EXPR)$";
string input = @"2+2*(3^5)
""halo""+""howjuicavocad""
foo.bar(baz).hello
=> (arg) arg+2";
RegexOptions options = RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline;
foreach (Match m in Regex.Matches(input, pattern, options))
{
Console.WriteLine("'{0}' found at index {1}.", m.Value, m.Index);
}
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for C#, please visit: https://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex(v=vs.110).aspx