DEV Community

Evan Lin
Evan Lin

Posted on • Originally published at evanlin.com on

Learning Golang REGEX with ChatGPT

title: [Learning Experience][Golang] Using ChatGPT for REGEX Tasks
published: false
date: 2023-06-09 00:00:00 UTC
tags: 
canonical_url: http://www.evanlin.com/go-regex-chatgpt/
---

![image-20230611001729736](http://www.evanlin.com/images/2022/image-20230611001729736.png)

# Preface

Like everyone else, I often use ChatGPT to help with coding problems, especially with Regular Expressions. Recently, I found that it's easier to succeed by using "positive assertion" rather than telling it what not to include. Or rather, Regular Expressions should be done with positive assertions in the first place.

## Original:

Enter fullscreen mode Exit fullscreen mode

// AddLineBreaksAroundURLs takes a string as input, finds URLs,
// and inserts a newline character before and after each URL.
// It returns the modified string.
func AddLineBreaksAroundURLs(input string) string {
re := regexp.MustCompile((https?:\/\/[^\s\p{Han}]+))
return re.ReplaceAllString(input, "\n$1\n")
}


### Found it couldn't handle:

Enter fullscreen mode Exit fullscreen mode

https://voyager.minedojo.org/。


When I found myself constantly telling ChatGPT to exclude the case ` https://voyager.minedojo.org/。`, it often got stuck and couldn't move forward. However, writing multiple test cases is actually quite helpful.

### Unit Testing code

Enter fullscreen mode Exit fullscreen mode

func TestAddLineBreaksAroundURLs(t *testing.T) {
tests := []struct {
input string
expected string
}{
{
input: "Check out this website https://example.com and this one http://another-example.com",
expected: "Check out this website \nhttps://example.com\n and this one \nhttp://another-example.com\n",
},
{
input: "Here is an URL with dot at the end https://voyager.minedojo.org/。",
expected: "Here is an URL with dot at the end \nhttps://voyager.minedojo.org/\n。",
},
{
input: "This is another test 可在https://voyager.minedojo.org/上訪問。",
expected: "This is another test 可在\nhttps://voyager.minedojo.org/\n上訪問。",
},
// Add more test cases here as needed
}

for _, tt := range tests {
    res := AddLineBreaksAroundURLs(tt.input)
    assert.Equal(t, tt.expected, res, "Should correctly insert line breaks around URLs")
}
Enter fullscreen mode Exit fullscreen mode

}


## Positive Assertion

Later, I decided to solve this problem using positive assertion.

Enter fullscreen mode Exit fullscreen mode

// AddLineBreaksAroundURLs takes a string as input, finds URLs,
// and inserts a newline character before and after each URL.
// It returns the modified string.
func AddLineBreaksAroundURLs(input string) string {
re := regexp.MustCompile((https?:\/\/[\w\/\?\&\.\-]+))
matches := re.FindAllString(input, -1)
for _, match := range matches {
input = strings.Replace(input, match, "\n" + match + "\n", 1)
}
return input
}


You can share the ChatGPT link via [this link](https://chat.openai.com/share/68599fd3-2ec7-4e6d-a410-42a1d52b1aac)

![image-20230611002225533](http://www.evanlin.com/images/2022/image-20230611002225533.png)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)