Skip to content

Commit ace5b1f

Browse files
authored
Handle missing optional matches (#204)
The Index trait for Captures panics if there is no match at a given index. Closes #193
1 parent fca19ae commit ace5b1f

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/operator/parse.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,12 @@ impl Parse {
3434
None => Ok(None),
3535
Some(capture) => {
3636
let mut values: Vec<data::Value> = Vec::with_capacity(self.fields.len());
37-
for i in 0..self.fields.len() {
37+
for item in capture.iter().skip(1) {
3838
// the first capture is the entire string
39-
values.push(data::Value::from_string(&capture[i + 1]));
39+
match item {
40+
None => values.push(data::Value::None),
41+
Some(match_) => values.push(data::Value::from_string(match_.as_str())),
42+
};
4043
}
4144
Ok(Some(values))
4245
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
query = """* |parse regex "(?P<url>.+)(?P<query>\\?.+)?""""
2+
input = """/track/?verbose=1&ip=1&_=1716389413340"""
3+
output = """
4+
[query=None] [url=/track/?verbose=1&ip=1&_=1716389413340]
5+
"""
6+
error = """
7+
"""

0 commit comments

Comments
 (0)