messagetemplates / messagetemplates-fsharp

Message templates - the ability to format named string values, and capture the properties
http://messagetemplates.org/
Apache License 2.0
18 stars 4 forks source link

Build a property extraction Regex #4

Closed adamchester closed 9 years ago

adamchester commented 9 years ago

This issue records an attempt to build a regular expression which can parse/match all the properties within a message template according the "spec" (which doesn't really exist yet, but it will be basically the same as Serilog).

(?<start>\{)
(?<destr>\@|\$)?
(?<property>[\w\.\[\]]+)
(?<align>,[^}:]+)?
(?<format>:[^}]+)?
(?<end>\})+

Tests

I sat at {@Chair.whatever.whatever} {{UHOH}}
Just biting '{@Fruit}' number "#{@Count:abc}"
Income was {Income} at {@Da te:d}
Welcome, customer #{$CustomerId,-10}, pleasure to see you
Welcome, customer #{CustomerId,-10:000000}, pleasure to see you
{place}, {0}
{1}, {Place}
{{blahblah}}

messsage templates property extraction regex

adamchester commented 9 years ago

Here is a link to an online (ECMAScript) version of the regex.

It looks like {{UHOH}} is a problem, as I understand the double-braces should be "escaped" into a single brace in the output.

messsage templates property extraction regex online highlights

adamchester commented 9 years ago

messsage templates property extraction regex debuggex visual

adamchester commented 9 years ago

Here is an updated Regex which matches {{ and }} so they can be replaced with { and }

([^{]
 (?<start_prop>\{)
 (?<destr>\@|\$)?
 (?<property>[\w\.\[\]]+)
 (?<align>,[^:}]+)?
 (?<format>:[^}]+)?
 (?<end_prop>\})+)
|
(?<double_open>\{\{)
|
(?<double_close>\}\})

with the input:

I sat at {@Chair.whatever.whatever} {{{{UHOH}}
Just biting '{@Fruit}' number "#{@Count:abc}"
Income was {Income} at {@Da te:d}
Welcome, customer #{$CustomerId,-10}, pleasure to see you
Welcome, customer #{CustomerId,-10:000000}, pleasure to see you
{place}, {0}
{1}, {Place}
{{blahblah}}

Replacement format: DO=${double_open}, DC=${double_close}, S=${start_prop}, D=${destr}, P=${property}, A=${align}, F=${format}, E=${end_prop}

Gives the output:

DO=, DC=, S={, D=@, P=Chair.whatever.whatever, A=, F=, E=}
DO={{, DC=, S=, D=, P=, A=, F=, E=
DO={{, DC=, S=, D=, P=, A=, F=, E=
DO=, DC=}}, S=, D=, P=, A=, F=, E=
DO=, DC=, S={, D=@, P=Fruit, A=, F=, E=}
DO=, DC=, S={, D=@, P=Count, A=, F=:abc, E=}
DO=, DC=, S={, D=, P=Income, A=, F=, E=}
DO=, DC=, S={, D=$, P=CustomerId, A=,-10, F=, E=}
DO=, DC=, S={, D=, P=CustomerId, A=,-10, F=:000000, E=}
DO=, DC=, S={, D=, P=place, A=, F=, E=}
DO=, DC=, S={, D=, P=0, A=, F=, E=}
DO=, DC=, S={, D=, P=1, A=, F=, E=}
DO=, DC=, S={, D=, P=Place, A=, F=, E=}
DO={{, DC=, S=, D=, P=, A=, F=, E=
DO=, DC=}}, S=, D=, P=, A=, F=, E=