Skip to main content
  1. Posts/

Writing Regex with Copilot

·754 words·4 mins
Chris Ayers
Author
Chris Ayers
I am a father, nerd, gamer, and speaker.

I had a real world example today that I wanted to share about copilot helping me with a coding problem. A friend reached out asking if I knew regex. Its been a bit, but I was happy to help because I thought we could figure it out.

The Problem
#

Her use case was that she had a string and she wanted to match a word only if another word appeared before it. She works at a University and is doing improvements to the CMS templates. They needed to do some updates to a Template only if it included certain tags. She was able to give me a sample that I could work with.

She was able to boil it down to a simple example. Given the sample, ‘I think puppies are super cute, but turtles are super cute too.’ make a regex that matches turtles only if puppies appears before it.

The Solution
#

Its been a while so I used GitHub Copilot Chat to help me out.

Given the sample, ‘I think puppies are super cute, but turtles are super cute too.’ Make a regex that matches turtles only if puppies appears before it.

Sure, here is a regular expression that matches “turtles” only if “puppies” appears before it in the text:

puppies.*turtles

This regular expression works as follows:

puppies matches the characters “puppies” literally. .* is a special sequence that matches any character (except for a newline) 0 or more times. turtles matches the characters “turtles” literally.

So, this regular expression will match any string that has “turtles” somewhere after “puppies”.

We are using regexer.com to test out the regexes. The result was close but we got in a back and forth refining the regex.

Testing regexes on regexer.com

I want to match and return only the word turtles, not additional text. To match and return only the word “turtles”, but only if “puppies” appears before it in the text, you can use a lookahead assertion in your regular expression. Here’s how you can do it:

(?<=puppies.*)(turtles)

This regular expression works as follows:

(?<=puppies.*) is a positive lookbehind assertion that checks for the presence of “puppies” followed by any characters before the main expression. (turtles) is the main expression that matches the word “turtles”. So, this regular expression will match the word “turtles” only if it appears somewhere after the word “puppies”, but it will only return “turtles”.

Awesome!

matching turtles

not matching turtles

Done, Or Are We?
#

My friend then threw a curveball at me. She wanted to match turtles only if puppies appears even if there are line breaks or other paragraphs. Like in this sample:

‘I think puppies are super cute,
but turtles are super cute too.’

Let’s see if we can modify the regex to match this.

Given the sample, ‘I think are super cute, but puppies turtles are super cute too.’ and the regex (?<=puppies.*)(turtles) I would like this to match turtles only if puppies appears even if there are line breaks or other paragraphs.. Like in this sample: ‘I think puppies are super cute, but turtles are super cute too.’

The regular expression (?<=puppies.*)(turtles) you provided uses a positive lookbehind (?<=puppies.*) to check if “turtles” is preceded by “puppies”. However, the . character does not match line breaks in regular expressions.

To match “turtles” only if “puppies” appears before it, even if there are line breaks or other paragraphs, you can use the [\s\S]* pattern instead of .*. The [\s\S] pattern matches any character including line breaks. Here’s how you can modify your regex:

(?<=puppies[\s\S]*)(turtles)

This will match “turtles” only if “puppies” appears before it, regardless of line breaks or other paragraphs. For example, it will match “turtles” in the following text:

I think puppies are super cute, but turtles are super cute too.

Lets try this out.

matching turtles same line
matching turtles different lines
not matching turtles

Everything looks like its working.

Conclusion
#

At this point, my friend had told me she had spent over an hour on stack overflow and other sites trying to figure this out. She had been hesitant to use GitHub Copilot but I think this was her aha moment. We literally solved her problem in under 5 minutes.

I have been using it for a while now and I am happy with the results. I don’t worry as much about syntax, I’m really more focused on WHAT I want to do and not HOW to do it. I think this is a game changer for me. I’m excited to see how this tool evolves.

If you haven’t tried out GitHub Copilot, I would recommend it. https://github.com/features/copilot

Related

Containerizing .NET - Part 2 - Considerations

·1976 words·10 mins
This is part 2 of the Containerizing .NET series. You can read the series of articles here: Containerizing .NET: Part 1 - A Guide to Containerizing .NET Applications Containerizing .NET: Part 2 - Considerations Considerations # Welcome to the second installment in our series on containerizing .NET applications. Building on the foundation laid in our first article-where we introduced Dockerfiles and the dotnet publish command-this piece delves into pivotal considerations for transitioning .NET applications into containers. As containers become a cornerstone of the ecosystem, understanding these factors is critical for developers aiming to enhance application deployment in containerized environments.

Dev Containers - Part 1

·1724 words·9 mins
This article is part of the Festive Tech Calendar 2023. For more articles in the series by other authors, visit https://festivetechcalendar.com/. Dev Containers can revolutionize the way we approach development environments, offering a fast, consistent setup across different projects. As a developer who uses Dev Containers in VS Code for various projects, I’ve experienced firsthand the benefits of having an environment that’s ready to go as soon as I clone a project.

Containerizing .NET - Part 1

·1515 words·8 mins
This article is part of C# Advent 2023. For more articles in the series by other authors, visit https://www.csadvent.christmas/. This is the first in a series of articles on containerizing .NET applications. We’ll explore how to containerize .NET applications using Dockerfiles and dotnet publish. Containers have become an essential part of the DevOps ecosystem, offering a lightweight, portable, and scalable solution for deploying applications. This process is crucial for developers looking to streamline app deployment in containerized environments, focusing on efficiency, security, compliance, and more.

Multiple Domains on GitHub Pages

Something I found out after moving from WordPress to GitHub Pages is that out of the box you can only host a single domain for a repository with GitHub Pages. This is a problem for me because I have a number of domains I was hosting at WordPress that I wanted to point at my GitHub Pages. Official Docs and the limitation # So officially, GitHub pages doesn’t support multiple domains. The docs here https://docs.github.com/en/pages/configuring-a-custom-domain-for-your-github-pages-site/troubleshooting-custom-domains-and-github-pages#custom-domain-names-that-are-unsupported state:

Customizing the Jekyll Theme

I haven’t done a lot with jekyll in the past, but I’m a big fan of Markdown everything. For me that usually means I’m taking notes in Markdown Obsidian, doing diagrams in mermaid in Azure DevOps or https://mermaid.live/. I’ve even started turning my talk slides into Markdown with a tool called MARP. Understanding when I use standard Markdown or some sort of templating language (jekyll uses Liquid) has been fun. I’ll do something in HTML or Markdown, then find out that Jekyll or my theme already has helpers to render that (like gists, videos, and figures). Sometimes rendering more advanced things takes a little tweaking of Jekyll and the theme.