Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion course-definition.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand All @@ -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.
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.
33 changes: 33 additions & 0 deletions stage_descriptions/multiple-matches-01-cj0.md
Original file line number Diff line number Diff line change
@@ -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.
40 changes: 40 additions & 0 deletions stage_descriptions/multiple-matches-02-ss2.md
Original file line number Diff line number Diff line change
@@ -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.
42 changes: 42 additions & 0 deletions stage_descriptions/multiple-matches-03-bo4.md
Original file line number Diff line number Diff line change
@@ -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
Loading