diff --git a/course-definition.yml b/course-definition.yml index 4b2c272..0d0c507 100644 --- a/course-definition.yml +++ b/course-definition.yml @@ -83,6 +83,14 @@ extensions: Along the way, you'll learn about extracting matched lines, formatting output, and managing line-by-line processing. + - slug: "multiple-matches" + name: "Multiple Matches" + description_markdown: | + In this challenge extension, you'll add support for printing the multiple matching results to your Grep implementation. + + Along the way, you'll learn about how to implement the `-o` flag. + + stages: - slug: "cq2" name: "Match a literal character" @@ -312,6 +320,7 @@ stages: marketing_md: |- In this stage, we'll add support for `{n,m}`, the [between n and m times](https://docs.microsoft.com/en-us/dotnet/standard/base-types/quantifiers-in-regular-expressions#match-between-n-and-m-times-nm) quantifier. + # Printing Matches - slug: "ku5" primary_extension_slug: "printing-matches" name: "Print a single matching line" @@ -324,4 +333,26 @@ stages: name: "Print multiple matching lines" difficulty: easy marketing_md: |- - In this stage, you'll add support for printing multiple input lines if they match the pattern. \ No newline at end of file + In this stage, you'll add support for printing multiple input lines if they match the pattern. + + # Multiple matches + - slug: "cj0" + primary_extension_slug: "multiple-matches" + name: "Print single match" + difficulty: medium + marketing_md: |- + In this stage, you'll add support for printing a single matching text to your grep implementation. + + - slug: "ss2" + primary_extension_slug: "multiple-matches" + name: "Print multiple matches" + difficulty: medium + marketing_md: |- + In this stage, you'll add support for printing multiple matching texts from a single line to your grep implementation. + + - slug: "bo4" + primary_extension_slug: "multiple-matches" + name: "Process multiple input lines" + difficulty: medium + marketing_md: |- + In this stage, you'll add support for processing multiple input lines to print all matching texts. \ No newline at end of file diff --git a/stage_descriptions/multiple-matches-01-cj0.md b/stage_descriptions/multiple-matches-01-cj0.md new file mode 100644 index 0000000..35547e2 --- /dev/null +++ b/stage_descriptions/multiple-matches-01-cj0.md @@ -0,0 +1,33 @@ +In this stage, you'll add support for printing a single matching text to your grep implementation. + +### The `-o` flag + +The `-o` flag, also known as the `only-matching` flag, is used to print only the matching texts in a separate line. + +Example usage: + +```bash +$ echo -ne "I have one cow" | grep -o -E 'cow' +cow +``` + +### Tests + +The tester will execute your program like this: + +```bash +$ echo -n "The king had 7 daughters" | ./your_program.sh -o -E "\d" +7 +``` + +If the input line does not match the specified pattern, your program must: +- Exit with the code 1 +- Exit with no printed output + +If the input line matches the specified pattern, your program must: +- Print the matched text to the standard output +- Exit with the code 0 + +### Notes + +- You only need to handle a single line of input and a single occurence of the pattern. We'll get to printing multiple matches and processing multiple lines in the later stages. \ No newline at end of file diff --git a/stage_descriptions/multiple-matches-02-ss2.md b/stage_descriptions/multiple-matches-02-ss2.md new file mode 100644 index 0000000..a7917e6 --- /dev/null +++ b/stage_descriptions/multiple-matches-02-ss2.md @@ -0,0 +1,40 @@ +In this stage, you'll add support for printing multiple matching texts from a single line to your grep implementation. + + +### Printing multiple matches + +If there are multiple matches present in the same line, each of them is printed on a separate line. + +```bash +# The '\d' pattern matches 1 and 0 separately +$ echo -n "The king had 10 children" | grep -o -P '\d' +1 +0 + +# The `\d\d` matches a pair of digits, so each pair (if found) is printed in its own line +$ echo -n "The king had 10 children" | grep -o -P '\d\d' +10 +``` + +### Tests + +The tester will execute your program like this: + +```bash +$ echo -e "jekyll and hyde" | ./your_program.sh -o -E "(jekyll|hyde)" +jekyll +hyde +``` + +If none of the lines match the specified pattern, your program must: +- Exit with the code 1 +- Exit with no printed output + +If at least one line matches the pattern, your program must: + +- Exit with the code 0 +- Print all the matching parts to the standard output + +### Notes + +- You only need to handle the case of a single input line. We will get to processing multiple input lines in later stages. \ No newline at end of file diff --git a/stage_descriptions/multiple-matches-03-bo4.md b/stage_descriptions/multiple-matches-03-bo4.md new file mode 100644 index 0000000..bb21d8a --- /dev/null +++ b/stage_descriptions/multiple-matches-03-bo4.md @@ -0,0 +1,42 @@ +In this stage, you'll add support for processing multiple input lines to print all matching texts. + +### Printing multiple matches from multiple lines + +If there are multiple lines in the input, matches from all lines are printed, each on their own line. + +```bash +# The '\d' pattern matches 1 and 0 separately in multiple lines +$ echo -ne "Line1: 10\nLine2: 42" | grep -o -P '\d' +1 +1 +0 +2 +4 +2 + +# The `\d\d` matches a pair of digits, so each pair (if found) is printed in its own line +$ echo -ne "Line01: 50\nLine02: 42" | grep -o -P '\d\d' +01 +50 +02 +42 +``` + +### Tests + +The tester will execute your program like this: + +```bash +$ echo -e "jekyll and hyde\nmickey mouse" | ./your_program.sh -o -E "(jekyll|hyde|mouse)" +jekyll +hyde +mouse +``` + +If none of the lines match the specified pattern, +- Your program should exit with the code 1 +- No output should be printed + +If at least one line matches the pattern, +- Your program should exit with the code 0 +- All the matching parts from all lines should be printed on their separate lines \ No newline at end of file