Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions internal/exercises/catalog.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,13 @@ projects:
- "Use Go's `time.Unix()` to convert an epoch to time."
- "Use `t.Unix()` to convert time back to epoch."
- "Remember Go’s `time.Parse` can help parse date strings."

- slug: regular-expressions
title: Regular Expressions
test_regex: ".*"
hints:
- Use the regexp package from the standard library.
- Compile the pattern with regexp.Compile or regexp.MustCompile.
- Use MatchString on the compiled regex to check if the input matches the pattern.
- Patterns like ^a.*z$ match strings starting with 'a' and ending with 'z'.
- See https://pkg.go.dev/regexp for more examples.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// regular-expressions.go
// Exercise: Regular Expressions
// Implement the function to match a string against a regular expression pattern.

package templates

import "regexp"

// MatchRegex returns true if the input matches the pattern, false otherwise.
func MatchRegex(pattern, input string) bool {
// TODO: implement this function
return false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package templates

import "testing"

func TestMatchRegex(t *testing.T) {
tests := []struct {
pattern string
input string
want bool
}{
{"^a.*z$", "abcz", true},
{"^a.*z$", "abz", true},
{"^a.*z$", "abc", false},
{"[0-9]+", "123", true},
{"[0-9]+", "abc", false},
{"hello|world", "hello", true},
{"hello|world", "world", true},
{"hello|world", "hi", false},
}
for _, tt := range tests {
got := MatchRegex(tt.pattern, tt.input)
if got != tt.want {
t.Errorf("MatchRegex(%q, %q) = %v; want %v", tt.pattern, tt.input, got, tt.want)
}
}
}