Chat GPT 4 - testing regular expression generation
Tue, 28 Mar 2023 by garethbrown
This was a quick test of Chat GPT 4's performance on a simple regular expression task I had coded manually earlier in the day where I needed to extract the text from a connection string property "Application Name" in a PostgreSQL connection string.
Seeing this Hacker News post regarding a service called regex.ai, I decided to see how Chat GPT 4 might have performed. In this case at least, I think Chat GPT has performed amazingly. I had earlier manually coded the regex
(Application\sName=)([^;]+), which fitted my use case well.
Chat GPT responded with
(?<=Application Name=)[^;]+. This solution works (as tested at regex101.com). It doesn't allow for the capture of the value for the
Application Name property but then I didn't ask it to. I'm especially impressed with its ability to explain the pattern.
Testing another generalised simple use case, giving the following prompt to Chat GPT 4 where we look to extract all property names from the connection string:
Write a regular expression to extract the property names from this PostgreSQL connection string: "PostgresSql": "Host=localhost;User ID=postgres;Password=xxxx;Database=test;Application Name=Test1234,Port=35432;Pooling=false;"
Yields the response with an explanation:
"This regular expression will match any sequence of alphabetic characters (upper or lower case) that are followed by an equal sign (=). The negative lookbehind (?<=[^\w]) ensures that the property name is not preceded by another word character."
A quick test on regex101.com shows this works perfectly.
I think that Chat GPT at version 4 has become a truly useful tool.
The use of any information, code samples, or product recommendations on this Website is entirely at your own risk, and we shall not be held liable for any loss or damage, direct or indirect, arising from or in connection with the use of this Website or the information provided herein.