Skip to content

Commit 47a6b41

Browse files
Merge pull request #181 from codecrafters-io/printing-matches
Grep/Printing Matches
2 parents 177c4e7 + f601b80 commit 47a6b41

File tree

4 files changed

+91
-0
lines changed

4 files changed

+91
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
course-definition-tester
22
.history/
3+
4+
# MacOS
5+
.DS_Store

course-definition.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@ extensions:
7676
Along the way, you'll learn about how to implement the `*` quantifier (zero or more), and bounded quantifiers.
7777
[1]: https://learn.microsoft.com/en-us/dotnet/standard/base-types/quantifiers-in-regular-expressions
7878
79+
- slug: "printing-matches"
80+
name: "Printing Matches"
81+
description_markdown: |
82+
In this challenge extension, you'll add support for printing matching lines to your Grep implementation.
83+
84+
Along the way, you'll learn about extracting matched lines, formatting output, and managing line-by-line processing.
85+
7986
stages:
8087
- slug: "cq2"
8188
name: "Match a literal character"
@@ -304,3 +311,17 @@ stages:
304311
difficulty: hard
305312
marketing_md: |-
306313
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.
314+
315+
- slug: "ku5"
316+
primary_extension_slug: "printing-matches"
317+
name: "Print a single matching line"
318+
difficulty: easy
319+
marketing_md: |-
320+
In this stage, you'll add support for printing a single input line if it matches the pattern.
321+
322+
- slug: "pz6"
323+
primary_extension_slug: "printing-matches"
324+
name: "Print multiple matching lines"
325+
difficulty: easy
326+
marketing_md: |-
327+
In this stage, you'll add support for printing multiple input lines if they match the pattern.
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 input line if it matches the pattern.
2+
3+
### Printing the input line
4+
5+
If the input line matches the pattern, Grep prints the line to the standard output.
6+
7+
Example usage:
8+
9+
```bash
10+
$ echo -n "apple" | ./your_program.sh -E "[^abc]"
11+
apple
12+
```
13+
14+
### Tests
15+
16+
The tester will execute your program like this:
17+
18+
```bash
19+
$ echo -n "dog" | ./your_program.sh -E "dog$"
20+
```
21+
22+
If the input line does not match the specified pattern, your program must:
23+
- Exit with the code 1
24+
- Exit with no printed output
25+
26+
If the input line matches the specified pattern, your program must:
27+
- Exit with the code 0
28+
- Print the input line to the standard output
29+
30+
31+
### Notes
32+
33+
- The tester will only test against a single input line. You don't need to process multiple input lines in this stage. We'll get to that in the next stage.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
In this stage, you'll add support for printing multiple input lines if they match the pattern.
2+
3+
### Printing multiple matching lines
4+
5+
If multiple input lines are provided, grep prints all the matching lines to the standard output.
6+
7+
Example usage:
8+
9+
```bash
10+
$ echo -ne "line1\nline_two\nline3" | grep -E "\d"
11+
line1
12+
line3
13+
```
14+
15+
### Tests
16+
17+
The tester will execute your program like this:
18+
19+
```bash
20+
$ echo -ne "first_line\n2nd_line\n3rd_line" | ./your_program.sh -E "\d"
21+
```
22+
23+
If none of the input lines match the specified pattern, your program must:
24+
- Exit with the code 1
25+
- Exit with no printed output
26+
27+
If at least one input line matches the specified pattern,
28+
- Exit with the code 0
29+
- Print all the matching input lines to the standard output
30+
31+
32+
### Notes
33+
34+
- You should print all the matching lines to the standard output.

0 commit comments

Comments
 (0)