using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = @"(\S*) && (\1[\.\[]).+";
string input = @"// Optional chaining for Javascript is now in stage 4:
// https://github.com/tc39/proposal-optional-chaining
// WARNING: This is NOT native to JS yet, so you will need to use a babel plugin to convert it:
// https://babeljs.io/docs/en/babel-plugin-proposal-optional-chaining
// Which means you can use this regex to find where in your codebase you can replace this:
user && user.profile && user.profile.email
// with this:
user?.profile?.email
// these should match because they have an object variable being accessed multiple times with a dot or opening bracket
data && data[query] && data[query].items && data[query].items.length > 0
const street = user.address && user.address.street;
// this should not match because they're not accessing the same object
data && form.user && api.authenticated";
RegexOptions options = 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