Automating Away Regex
Regular expressions are a potent tool for pattern matching and text manipulation.
Codeium, as an free AI Code Generator, takes regex to the next level by offering real-time suggestions and an in-IDE chat. Today, we'll see how Codeium's in-IDE chat will handle crafting regular expressions.
To evaluate the responses from Codeium, we'll use an existing regex website, regex101, to evaluate Codeium's output.
Emails
For the initial test, I prompted Codeium's chat to generate an email pattern in python. The AI responded with the following:
import re
email_regex = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
# Example usage:
email = 'example@email.com'
if re.match(email_regex, email):
print('Valid email address')
else:
print('Invalid email address')
Let's see how it stacks up to regex101.
Matched
name@company.com
firstname+lastname@example.com
name@company.co.jp
name@c.cc
Not Matched
name@cc
email@company
name
Social Security Numbers
Let's now try asking Codeium's chat if it can generate a regex for Social Secuirty Numbers. The AI responded with:
import re
pattern = r'\d{3}-\d{2}-\d{4}'
ssn = '123-45-6789'
if re.match(pattern, ssn):
print("Valid social security number")
else:
print("Invalid social security number")
Let's see it's performance against regex101.
Matched
843-29-2913
289-20-2812
929-29-8481
Not Matched
188 34 4526
aaa-aa-aaaa
939-23-3921
In conclusion, Codeium demonstrated quite the capability in generating regular expressions. Thopugh not guaranteed to always be 100% perfect, the ability to ask Codeium chat with to come up with regex workflows ensures productive coding experience.