This is a starting point for Go solutions to the "Build Your Own grep" Challenge.
Regular expressions
(Regexes, for short) are patterns used to match character combinations in
strings. grep
is a CLI tool for
searching using Regexes.
In this challenge you'll build your own implementation of grep
. Along the way
we'll learn about Regex syntax, how parsers/lexers work, and how regular
expressions are evaluated.
Note: If you're viewing this repo on GitHub, head over to codecrafters.io to try the challenge.
The entry point for your grep
implementation is in cmd/mygrep/main.go
. Study
and uncomment the relevant code, and push your changes to pass the first stage:
git add .
git commit -m "pass 1st stage" # any msg
git push origin master
Time to move on to the next stage!
Note: This section is for stages 2 and beyond.
- Ensure you have
go (1.19)
installed locally - Run
./your_program.sh
to run your program, which is implemented incmd/mygrep/main.go
. - Commit your changes and run
git push origin master
to submit your solution to CodeCrafters. Test output will be streamed to your terminal. - C Implementation Reference
/* match: search for regexp anywhere in text */
int match(char *regexp, char *text)
{
if (regexp[0] == '^')
return matchhere(regexp+1, text);
do { /* must look even if string is empty */
if (matchhere(regexp, text))
return 1;
} while (*text++ != '\0');
return 0;
}
/* matchhere: search for regexp at beginning of text */
int matchhere(char *regexp, char *text)
{
if (regexp[0] == '\0')
return 1;
if (regexp[1] == '*')
return matchstar(regexp[0], regexp+2, text);
if (regexp[0] == '$' && regexp[1] == '\0')
return *text == '\0';
if (*text!='\0' && (regexp[0]=='.' || regexp[0]==*text))
return matchhere(regexp+1, text+1);
return 0;
}
/* matchstar: search for c*regexp at beginning of text */
int matchstar(int c, char *regexp, char *text)
{
do { /* a * matches zero or more instances */
if (matchhere(regexp, text))
return 1;
} while (*text != '\0' && (*text++ == c || c == '.'));
return 0;
}