Skip to content
Open
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
15 changes: 6 additions & 9 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
// swift-tools-version: 6.2
// The swift-tools-version declares the minimum version of Swift required to build this package.

// swift-tools-version: 6.0
import PackageDescription

let package = Package(
name: "fledge-regex",
name: "fledge-plugin-regex",
platforms: [.macOS(.v13)],
targets: [
// Targets are the basic building blocks of a package, defining a module or a test suite.
// Targets can depend on other targets in this package and products from dependencies.
.executableTarget(
name: "fledge-regex"
),
.target(name: "RegexLib", path: "Sources/RegexLib"),
.executableTarget(name: "fledge-regex", dependencies: ["RegexLib"], path: "Sources/fledge-regex"),
.testTarget(name: "RegexTests", dependencies: ["RegexLib"], path: "Tests"),
]
)
134 changes: 134 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
# fledge-plugin-regex

A [fledge](https://github.com/CorvidLabs/fledge) plugin for testing, matching, replacing, explaining, and escaping regular expressions from the command line.

## Install

```bash
fledge plugins install regex
```

## Commands

### test

Test whether a pattern matches an input string. Shows matched text, offsets, and capture groups.

```bash
# Basic match
fledge regex test '\d{3}-\d{4}' '555-1234'

# Case-insensitive
fledge regex test -i 'hello' 'Hello World'

# Global (all matches) with capture groups
fledge regex test -g '(\w+)@(\w+)' 'a@b c@d'
```

### match

Find matching lines in a file or stdin. Similar to grep.

```bash
# Match lines in a file
fledge regex match 'TODO' src/main.swift

# Count matches only
fledge regex match --count 'func\s' lib.swift

# Invert match (show non-matching lines)
fledge regex match -v '^\s*$' file.txt

# JSON output
fledge regex match --json '\d+' data.txt

# From stdin
cat log.txt | fledge regex match 'ERROR'
```

### replace

Replace pattern matches in a file or stdin.

```bash
# Replace first match per line
fledge regex replace 'foo' 'bar' file.txt

# Replace all matches per line
fledge regex replace -g 'old' 'new' file.txt

# Modify file in place
fledge regex replace -g 'v1' 'v2' --in-place config.txt

# With capture group references ($1, $2, ...)
fledge regex replace '(\w+)@(\w+)' '$2/$1' file.txt

# From stdin
echo 'hello world' | fledge regex replace 'world' 'there'
```

### explain

Break down a regex pattern into human-readable tokens.

```bash
fledge regex explain '^\d{3}-\d{4}$'
# Output:
# ^ start of string
# \d digit [0-9]
# {3} exactly 3
# - literal '-'
# \d digit [0-9]
# {4} exactly 4
# $ end of string
```

### escape

Escape special regex characters in a string so it can be used as a literal pattern.

```bash
fledge regex escape 'hello (world)'
# Output: hello \(world\)

fledge regex escape 'price: $5.00'
# Output: price: \$5\.00
```

## Flags

| Flag | Commands | Description |
|------|----------|-------------|
| `-g`, `--global` | test, replace | Find/replace all matches |
| `-i`, `--case-insensitive` | test, match, replace | Case-insensitive matching |
| `--count` | match | Show only match count |
| `-v`, `--invert` | match | Show non-matching lines |
| `--json` | match | JSON output |
| `--in-place` | replace | Modify file in place |

## Development

Build:

```bash
swift build
```

Run tests:

```bash
swift test
```

## Architecture

The plugin is split into two targets:

- **RegexLib** (`Sources/RegexLib/`) -- Pure logic: regex compilation, pattern explanation, string escaping, argument parsing, and error types. Fully testable with no I/O.
- **fledge-regex** (`Sources/fledge-regex/`) -- CLI entry point: color output, file I/O, stdin handling, dispatch, and help text. Imports RegexLib.

Tests live in `Tests/` and cover all public RegexLib functions.

## License

See [CorvidLabs/fledge](https://github.com/CorvidLabs/fledge) for license information.
Loading