For software development and maintenance, contact me at contact@appsoftware.com or via appsoftware.com


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.

image.png

image.png

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:

(?<=[^\\w])([A-Za-z ]+)(?==)

"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."

image.png

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 information provided on this Website is for general informational and educational purposes only. While we strive to provide accurate and up-to-date information, we make no warranties or representations, express or implied, as to the accuracy, completeness, reliability, or suitability of the content, including code samples and product recommendations, presented on this Website.

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.
UI block loader
One moment please ...