Skip to content

Commit 66a82bf

Browse files
Merge pull request #179 from codecrafters-io/multiple-matches
Grep/Multiple Matches
2 parents 4db1d18 + 7e146bc commit 66a82bf

File tree

4 files changed

+147
-1
lines changed

4 files changed

+147
-1
lines changed

course-definition.yml

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,14 @@ extensions:
8383
8484
Along the way, you'll learn about extracting matched lines, formatting output, and managing line-by-line processing.
8585
86+
- slug: "multiple-matches"
87+
name: "Multiple Matches"
88+
description_markdown: |
89+
In this challenge extension, you'll add support for printing the multiple matching results to your Grep implementation.
90+
91+
Along the way, you'll learn about how to implement the `-o` flag.
92+
93+
8694
stages:
8795
- slug: "cq2"
8896
name: "Match a literal character"
@@ -312,6 +320,7 @@ stages:
312320
marketing_md: |-
313321
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.
314322
323+
# Printing Matches
315324
- slug: "ku5"
316325
primary_extension_slug: "printing-matches"
317326
name: "Print a single matching line"
@@ -324,4 +333,26 @@ stages:
324333
name: "Print multiple matching lines"
325334
difficulty: easy
326335
marketing_md: |-
327-
In this stage, you'll add support for printing multiple input lines if they match the pattern.
336+
In this stage, you'll add support for printing multiple input lines if they match the pattern.
337+
338+
# Multiple matches
339+
- slug: "cj0"
340+
primary_extension_slug: "multiple-matches"
341+
name: "Print single match"
342+
difficulty: medium
343+
marketing_md: |-
344+
In this stage, you'll add support for printing a single matching text to your grep implementation.
345+
346+
- slug: "ss2"
347+
primary_extension_slug: "multiple-matches"
348+
name: "Print multiple matches"
349+
difficulty: medium
350+
marketing_md: |-
351+
In this stage, you'll add support for printing multiple matching texts from a single line to your grep implementation.
352+
353+
- slug: "bo4"
354+
primary_extension_slug: "multiple-matches"
355+
name: "Process multiple input lines"
356+
difficulty: medium
357+
marketing_md: |-
358+
In this stage, you'll add support for processing multiple input lines to print all matching texts.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
In this stage, you'll add support for printing a single matching text to your grep implementation.
2+
3+
### The `-o` flag
4+
5+
The `-o` flag, also known as the `only-matching` flag, is used to print only the matching texts in a separate line.
6+
7+
Example usage:
8+
9+
```bash
10+
$ echo -ne "I have one cow" | grep -o -E 'cow'
11+
cow
12+
```
13+
14+
### Tests
15+
16+
The tester will execute your program like this:
17+
18+
```bash
19+
$ echo -n "The king had 7 daughters" | ./your_program.sh -o -E "\d"
20+
7
21+
```
22+
23+
If the input line does not match the specified pattern, your program must:
24+
- Exit with the code 1
25+
- Exit with no printed output
26+
27+
If the input line matches the specified pattern, your program must:
28+
- Print the matched text to the standard output
29+
- Exit with the code 0
30+
31+
### Notes
32+
33+
- 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.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
In this stage, you'll add support for printing multiple matching texts from a single line to your grep implementation.
2+
3+
4+
### Printing multiple matches
5+
6+
If there are multiple matches present in the same line, each of them is printed on a separate line.
7+
8+
```bash
9+
# The '\d' pattern matches 1 and 0 separately
10+
$ echo -n "The king had 10 children" | grep -o -P '\d'
11+
1
12+
0
13+
14+
# The `\d\d` matches a pair of digits, so each pair (if found) is printed in its own line
15+
$ echo -n "The king had 10 children" | grep -o -P '\d\d'
16+
10
17+
```
18+
19+
### Tests
20+
21+
The tester will execute your program like this:
22+
23+
```bash
24+
$ echo -e "jekyll and hyde" | ./your_program.sh -o -E "(jekyll|hyde)"
25+
jekyll
26+
hyde
27+
```
28+
29+
If none of the lines match the specified pattern, your program must:
30+
- Exit with the code 1
31+
- Exit with no printed output
32+
33+
If at least one line matches the pattern, your program must:
34+
35+
- Exit with the code 0
36+
- Print all the matching parts to the standard output
37+
38+
### Notes
39+
40+
- You only need to handle the case of a single input line. We will get to processing multiple input lines in later stages.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
In this stage, you'll add support for processing multiple input lines to print all matching texts.
2+
3+
### Printing multiple matches from multiple lines
4+
5+
If there are multiple lines in the input, matches from all lines are printed, each on their own line.
6+
7+
```bash
8+
# The '\d' pattern matches 1 and 0 separately in multiple lines
9+
$ echo -ne "Line1: 10\nLine2: 42" | grep -o -P '\d'
10+
1
11+
1
12+
0
13+
2
14+
4
15+
2
16+
17+
# The `\d\d` matches a pair of digits, so each pair (if found) is printed in its own line
18+
$ echo -ne "Line01: 50\nLine02: 42" | grep -o -P '\d\d'
19+
01
20+
50
21+
02
22+
42
23+
```
24+
25+
### Tests
26+
27+
The tester will execute your program like this:
28+
29+
```bash
30+
$ echo -e "jekyll and hyde\nmickey mouse" | ./your_program.sh -o -E "(jekyll|hyde|mouse)"
31+
jekyll
32+
hyde
33+
mouse
34+
```
35+
36+
If none of the lines match the specified pattern,
37+
- Your program should exit with the code 1
38+
- No output should be printed
39+
40+
If at least one line matches the pattern,
41+
- Your program should exit with the code 0
42+
- All the matching parts from all lines should be printed on their separate lines

0 commit comments

Comments
 (0)