Skip to content

Commit

Permalink
examples and readme
Browse files Browse the repository at this point in the history
  • Loading branch information
yuanbohan committed Jun 7, 2024
1 parent 66566ad commit 30c5d0b
Show file tree
Hide file tree
Showing 8 changed files with 500 additions and 37 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright [yyyy] [name of copyright owner]
Copyright [2024] [yuanbohan]

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
81 changes: 81 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,88 @@ Rust port of Elastic Grok processor, inspired by [grok-go][grok-go] and [grok][g

## Example

### Only with default patterns

```rust
let grok = Grok::default();
let pattern = grok
// USERNAME are defined in grok-patterns
.compile("%{USERNAME}", false)
.unwrap();
let result = pattern.parse("admin [email protected]").unwrap();
println!("{:#?}", result);
```

the output is:

```text
{
"USERNAME": String(
"admin",
),
}
```

### With user-defined patterns

```rust
let mut grok = Grok::default();
grok.add_pattern("NAME", r"[A-z0-9._-]+");
let pattern = grok.compile("%{NAME}", false).unwrap();
let result = pattern.parse("admin").unwrap();
println!("{:#?}", result);
```

the output is:

```text
{
"NAME": String(
"admin",
),
}
```

### With `named_capture_only` is true

```rust
let grok = Grok::default();
let pattern = grok
.compile("%{USERNAME} %{EMAILADDRESS:email}", true)
.unwrap();
let result = pattern.parse("admin [email protected]").unwrap();
println!("{:#?}", result);
```

the output is:

```text
{
"email": String(
"[email protected]",
),
}
```

### With type

```rust
let mut grok = Grok::default();
grok.add_pattern("NUMBER", r"\d+");

let pattern = grok.compile("%{NUMBER:digit:int}", false).unwrap();
let result = pattern.parse("hello 123").unwrap();
println!("{:#?}", result);
```

the output is:

```text
{
"digit": Int(
123,
),
}
```

## Elastic Grok compliance
Expand Down
9 changes: 9 additions & 0 deletions examples/add_pattern.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use grok_rs::Grok;

fn main() {
let mut grok = Grok::default();
grok.add_pattern("NAME", r"[A-z0-9._-]+");
let pattern = grok.compile("%{NAME}", false).unwrap();
let result = pattern.parse("admin").unwrap();
println!("{:#?}", result);
}
12 changes: 12 additions & 0 deletions examples/default_patterns.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use grok_rs::Grok;

fn main() {
let grok = Grok::default();
let pattern = grok
// USERNAME and EMAILADDRESS are defined in grok-patterns
.compile("%{USERNAME}", false)
.unwrap();

let result = pattern.parse("admin [email protected]").unwrap();
println!("{:#?}", result);
}
10 changes: 10 additions & 0 deletions examples/named_capture_only.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use grok_rs::Grok;

fn main() {
let grok = Grok::default();
let pattern = grok
.compile("%{USERNAME} %{EMAILADDRESS:email}", true)
.unwrap();
let result = pattern.parse("admin [email protected]").unwrap();
println!("{:#?}", result);
}
10 changes: 10 additions & 0 deletions examples/type.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use grok_rs::Grok;

fn main() {
let mut grok = Grok::default();
grok.add_pattern("NUMBER", r"\d+");

let pattern = grok.compile("%{NUMBER:digit:int}", false).unwrap();
let result = pattern.parse("hello 123").unwrap();
println!("{:#?}", result);
}
Loading

0 comments on commit 30c5d0b

Please sign in to comment.