Go Pattern Matching with Regular Expressions
Pattern matching is a fundamental concept in computer science, allowing you to search for specific sequences within larger strings. This challenge asks you to implement a simple pattern matching function in Go using regular expressions, enabling you to identify if a given pattern exists within a target string. This is useful for tasks like data validation, text parsing, and search functionality.
Problem Description
You are tasked with creating a Go function called MatchPattern that takes two strings as input: a pattern and a text. The function should use regular expressions to determine if the pattern exists anywhere within the text. The function should return a boolean value: true if the pattern is found, and false otherwise.
Key Requirements:
- The function must use the
regexppackage in Go. - The function should handle invalid regular expression patterns gracefully (see Edge Cases).
- The function should be case-sensitive.
- The pattern can be any valid regular expression.
Expected Behavior:
The function should compile the regular expression pattern and then attempt to match it against the input text. If a match is found, it should return true. If no match is found, or if the pattern is invalid, it should return false.
Edge Cases to Consider:
- Empty Pattern: If the
patternis an empty string (""), it should returnfalse. An empty regex pattern doesn't match anything meaningful in this context. - Empty Text: If the
textis an empty string (""), it should returnfalseunless the pattern is also empty (handled above). - Invalid Regular Expression: If the
patternis not a valid regular expression (e.g., contains syntax errors), the function should returnfalsewithout panicking. Useregexp.Compile's error return to handle this. - Special Characters: The pattern should correctly handle special characters in regular expressions (e.g.,
.,*,+,?,[],(),^,$).
Examples
Example 1:
Input: pattern = "abc", text = "xyzabc123"
Output: true
Explanation: The pattern "abc" is found within the text "xyzabc123".
Example 2:
Input: pattern = "def", text = "xyzabc123"
Output: false
Explanation: The pattern "def" is not found within the text "xyzabc123".
Example 3:
Input: pattern = "a.*c", text = "xyzabc123"
Output: true
Explanation: The pattern "a.*c" (a followed by any characters, followed by c) is found within the text.
Example 4:
Input: pattern = "[0-9]+", text = "abc123def"
Output: true
Explanation: The pattern "[0-9]+" (one or more digits) is found within the text.
Example 5:
Input: pattern = "", text = "abc"
Output: false
Explanation: An empty pattern does not match.
Example 6:
Input: pattern = "invalid[regex", text = "abc"
Output: false
Explanation: The pattern is an invalid regular expression.
Constraints
- The length of the
patternstring will be between 1 and 100 characters (inclusive), unless it's an empty string. - The length of the
textstring will be between 0 and 1000 characters (inclusive). - The function must compile and run within 1 second.
- The input strings will only contain ASCII characters.
Notes
Consider using the regexp.Compile function to compile the regular expression pattern. Remember to handle the error returned by regexp.Compile to gracefully manage invalid patterns. The MatchString method of the compiled regular expression can then be used to check for a match within the text. Focus on correctness and error handling.