diff --git a/internal/exercises/catalog.yaml b/internal/exercises/catalog.yaml index e8007e8..f6b7b89 100644 --- a/internal/exercises/catalog.yaml +++ b/internal/exercises/catalog.yaml @@ -237,6 +237,16 @@ projects: - "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. - slug: 37_sorting_by_functions title: "Sorting by Functions" difficulty: beginner diff --git a/internal/exercises/solutions/115_regular_expressions/regular_expressions.go b/internal/exercises/solutions/115_regular_expressions/regular_expressions.go new file mode 100644 index 0000000..4768a6e --- /dev/null +++ b/internal/exercises/solutions/115_regular_expressions/regular_expressions.go @@ -0,0 +1,16 @@ +// regular_expressions_solution.go +// Solution for: Regular Expressions +package regular_expressions + +import ( + "regexp" +) + +// MatchRegex returns true if the input matches the pattern, false otherwise. +func MatchRegex(pattern, input string) bool { + re, err := regexp.Compile(pattern) + if err != nil { + return false + } + return re.MatchString(input) +} diff --git a/internal/exercises/templates/115_regular_expressions/regular_expressions.go b/internal/exercises/templates/115_regular_expressions/regular_expressions.go new file mode 100644 index 0000000..bd415b9 --- /dev/null +++ b/internal/exercises/templates/115_regular_expressions/regular_expressions.go @@ -0,0 +1,15 @@ +// regular_expressions.go +// Exercise: Regular Expressions +// Implement the function to match a string against a regular expression pattern. + +package regular_expressions + +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 +} diff --git a/internal/exercises/templates/115_regular_expressions/regular_expressions_test.go b/internal/exercises/templates/115_regular_expressions/regular_expressions_test.go new file mode 100644 index 0000000..8f83b74 --- /dev/null +++ b/internal/exercises/templates/115_regular_expressions/regular_expressions_test.go @@ -0,0 +1,28 @@ +package regular_expressions + +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) + } + } +}