https://github.com/kei-k23/g-grep
G-grep is a small regular expression (regex) engine that is written in Go
https://github.com/kei-k23/g-grep
go regex regular-expression
Last synced: 3 months ago
JSON representation
G-grep is a small regular expression (regex) engine that is written in Go
- Host: GitHub
- URL: https://github.com/kei-k23/g-grep
- Owner: Kei-K23
- Created: 2024-08-28T13:27:41.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-08-28T13:44:04.000Z (almost 2 years ago)
- Last Synced: 2025-10-15T03:00:16.494Z (8 months ago)
- Topics: go, regex, regular-expression
- Language: Go
- Homepage:
- Size: 2.93 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# `G-Grep` - A Custom grep -E Implementation in Go
## Overview
`G-Grep` is a custom implementation of the grep -E command in `Go`. It reads input from stdin and matches it against a specified pattern, returning success (exit code 0) if a match is found, and failure (exit code 1) otherwise. This program supports some basic regular expression features, including `^, $, ?, +, ., character classes ([]), and backreferences`.
## Features
`Pattern Matching`: Matches lines based on the provided pattern.
`Anchors`: Supports ^ (beginning of line) and $ (end of line).
`Character Classes`: Supports character classes with negation ([^]).
`Quantifiers`: Supports ? (zero or one), + (one or more), and . (any single character).
`Backreferences`: Supports backreferences like \1.
`Escape Sequences`: Supports \d (digits) and \w (word characters).
## Usage
```bash
echo | ./g-grep -E
```
### Examples
1. Match any line containing the word "Go":
```bash
echo "I love Go programming" | ./g-grep -E "Go"
```
2. Match lines that start with "Hello":
```bash
echo "Hello, World!" | ./g-grep -E "^Hello"
```
3. Match lines that end with "end":
```bash
echo "This is the end" | ./g-grep -E "end$"
```
4. Match lines with a digit:
```bash
echo "The year is 2024" | ./g-grep -E "\d"
```
5. Match lines with a word character:```
```bash
echo "This_is_a_word" | ./g-grep -E "\w"
```
6. Match lines with an optional character (zero or one occurrence):
```bash
echo "color" | ./g-grep -E "colou?r"
```
7. Match lines with one or more occurrences of a character:
```bash
echo "aaa" | ./g-grep -E "a+"
```
8. Match lines using a backreference:
```bash
echo "abcabc" | ./g-grep -E "(abc)\1"
```
### Exit Codes
- 0: A match was found.
- 1: No match was found.
- 2: An error occurred (e.g., incorrect usage or pattern syntax).
## Installation
Ensure you have Go installed on your system.
1. Clone this repository:
```bash
git clone https://github.com/yourusername/g-grep.git
```
2. Build the program:
```bash
cd g-grep
go build -o g-grep
```
## Contributing
Feel free to contribute to this project by opening issues, suggesting features, or submitting pull requests.
## Acknowledgments
This project is inspired by the grep -E command and aims to provide a basic understanding of pattern matching and regular expressions in `Go`.