Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can do this with two regular expressions and a recursive function with <strong>one caveat</strong>: special characters must be escaped. From what I can see, <code>"="</code>, <code>"["</code> and <code>"]"</code> have special meaning, so you must insert a <code>"\"</code> before those characters if you want them to appear as part of your property value. Note that commas are not considered "special". A comma before a <code>"property="</code> string is ignored, but otherwise they are treated in no special way (and, in fact, are optional between properties).</p> <h2>Input</h2> <pre class="lang-text prettyprint-override"><code>ObjectType [ property1=value1,val\=value2 property2=value2 \[property2\=this is not an object\], property3= AnotherObjectType [property4=some value4]] </code></pre> <h2>Regular Expressions</h2> <p>The regex for discovering "complex" types (beginning with a type name followed by square brackets). The regex includes a mechanism for balancing square brackets to make sure that each open bracket is paired with a close bracket (so that the match does not end too soon or too late):</p> <pre class="lang-regex prettyprint-override"><code>^\s*(?&lt;TypeName&gt;\w+)\s*\[(?&lt;Properties&gt;([^\[\]]|\\\[|\\\]|(?&lt;!\\)\[(?&lt;Depth&gt;)|(?&lt;!\\)\](?&lt;-Depth&gt;))*(?(Depth)(?!)))\]\s*$ </code></pre> <p>The regex for discovering properties within a complex type. Note that this also includes balanced square brackets to ensure that the properties of a sub-complex type are not accidentally consumed by the parent.</p> <pre class="lang-regex prettyprint-override"><code>(?&lt;PropertyName&gt;\w+)\s*=\s*(?&lt;PropertyValue&gt;([^\[\]]|\\\[|\\\]|(?&lt;!\\)\[(?&lt;Depth&gt;)|(?&lt;!\\)\](?&lt;-Depth&gt;))*?(?(Depth)(?!))(?=$|(?&lt;!\\)\]|,?\s*\w+\s*=)) </code></pre> <h2>Code</h2> <pre><code>private static Regex ComplexTypeRegex = new Regex( @"^\s*(?&lt;TypeName&gt;\w+)\s*\[(?&lt;Properties&gt;([^\[\]]|\\\[|\\\]|(?&lt;!\\)\[(?&lt;Depth&gt;)|(?&lt;!\\)\](?&lt;-Depth&gt;))*(?(Depth)(?!)))\]\s*$" ); private static Regex PropertyRegex = new Regex( @"(?&lt;PropertyName&gt;\w+)\s*=\s*(?&lt;PropertyValue&gt;([^\[\]]|\\\[|\\\]|(?&lt;!\\)\[(?&lt;Depth&gt;)|(?&lt;!\\)\](?&lt;-Depth&gt;))*?(?(Depth)(?!))(?=$|(?&lt;!\\)\]|,?\s*\w+\s*=))" ); private static string Input = @"ObjectType" + "\n" + @"[" + "\n" + @" property1=value1,val\=value2 " + "\n" + @" property2=value2 \[property2\=this is not an object\], property3=" + "\n" + @" AnotherObjectType [property4=some " + "\n" + @"value4]]"; static void Main( string[] args ) { Console.Write( Process( 0, Input ) ); Console.WriteLine( "\n\nPress any key..." ); Console.ReadKey( true ); } private static string Process( int level, string input ) { var l_complexMatch = ComplexTypeRegex.Match( input ); var l_indent = string.Join( "", Enumerable.Range( 0, level * 3 ).Select( i =&gt; " " ).ToArray() ); var l_output = new StringBuilder(); l_output.AppendLine( l_indent + l_complexMatch.Groups["TypeName"].Value ); foreach ( var l_match in PropertyRegex.Matches( l_complexMatch.Groups["Properties"].Value ).Cast&lt;Match&gt;() ) { l_output.Append( l_indent + "@" + l_match.Groups["PropertyName"].Value + " = " ); var l_value = l_match.Groups["PropertyValue"].Value; if ( Regex.IsMatch( l_value, @"(?&lt;!\\)\[" ) ) { l_output.AppendLine(); l_output.Append( Process( level + 1, l_value ) ); } else { l_output.AppendLine( "\"" + l_value + "\"" ); } } return l_output.ToString(); } </code></pre> <h2>Output</h2> <pre class="lang-text prettyprint-override"><code>ObjectType @property1 = "value1,val\=value2 " @property2 = "value2 \[property2\=this is not an object\]" @property3 = AnotherObjectType @property4 = "some value4" </code></pre> <hr> <p>If you cannot escape the delimiters, then I doubt even a human could parse such a string. For example, how would a human reliably know whether the value of property 3 should be considered a literal string or a complex type?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload