Skip to content

Copilot Regular Expression Analysis and Generation

Copilot is an AI-powered programming assistant that can help with regular expression analysis and generation. This section will go over how Copilot can assist with regular expressions and provide examples of how to use it for analysis and generation.

Regular Expression Analysis

Copilot can analyze existing regular expressions to provide insights and suggestions. It can help you understand the structure and behavior of a regex pattern, identify potential issues or improvements, and provide explanations for different components.

To analyze a regular expression using Copilot, simply provide the regex pattern as input. For example, I set up an email validator in Golang and asked Copilot to analyze and validate my regular expression.

Prompt Copilot for Regular Expression Analysis

Based on my selected function. How valid is my regular expression if I wanted to parse out emails provided as input?

regex analysis

In the example Copilot gave me some insights about the regular expression I provided. It explained the different components of the regex pattern, highlighted potential issues when prompted.

Regular Expression Generation

Copilot can also generate regular expressions based on provided examples or desired patterns. This feature is particularly useful when you need to create complex regex patterns but are not familiar with the syntax or specific components.

To generate a regular expression using Copilot, you can provide it with a prompt based on the situation in which you need the regex pattern for.

Another way is what we have for analysis. Copilot can generate a regular expression based on context you provide such as another regular expression that you would want to improve on.

Here is an example from the previous section, but this time we prompt Copilot to generate a regular expression for us.

Prompt Copilot for Regular Expression Generation

Can you generate a regular expression pattern that is appropriate for validating emails that get passed in?

regex generation

Copilot generated a regular expression based on the context I provided with information about the components and structure of the pattern.

Copilot also made a note about the generated regex pattern can be used for most use cases. However, it also notes that it may not cover all email patterns stated in RFC 5322.

Since Copilot mentioned RFC 5322, I asked Copilot what is RFC 5322 and how does it relate to the validation of emails.

Prompt Copilot for RFC 5322 Explanation

What is RFC 5322 when it comes to valid email patterns?

regex rfc5322

In this case Copilot gave a brief explanation about RFC 5322. Based on the highlighted pattern I have in my Golang code from my previous prompt, it provided context on how it only validates a subset of email patterns and does not meet the full RFC 5322 standard. It also assured that the highlighted pattern is still valid for most email patterns.

Since we are only covering a subset of patterns that RFC 5322 deems valid. I now asked Copilot to generate a regular expression pattern that meets the RFC 5322 standard.

Prompt Copilot for RFC 5322 Standard Regular Expression Generation

Can you generate a regular expression pattern that meets the RFC 5322 standard for validating emails that get passed in?

regex complex pattern

Copilot provided me a new complex pattern that that meets more of the standard in place for RFC 5322. As seen in the image, it is quite a lengthy pattern.

Copilot also mentions that, while the regex pattern may meet RFC 5322 standards, it may not be practical for applications. It recommends that a simpler pattern will do the job in most cases in regards to validating the majority of email patterns.

What are your thoughts on the regex pattern generated originally for email validate versus the one generated to meet RFC 5322 standards?