我有以下数据
let data = r#"title1
title1 line1
title1 line2
sep/
title2
title2 line1
title2 line2
title2 line3
sep/
title3
title3 line1
sep/"#;
基本上它代表三个条目:
struct Entry {
title: String,
body: String,
}
每个条目都有一个标题和一个正文。标题使用单行(不包括行尾),正文使用所有后续行,直到遇到分隔线 ( sep/
)。我想要的结果是一个条目向量。如何使用 nom 来解析它?我对 nom 很陌生,我不能让这些部分一起工作并形成一个工作解析器。以下是我所拥有的:
use nom::IResult;
use nom::branch::alt;
use nom::bytes::complete::{tag, take_until, is_not, is_a};
use nom::error::ErrorKind::ParseTo;
use nom::sequence::{pair, tuple, delimited, terminated};
use nom::combinator::opt;
use nom::error::{Error, ErrorKind};
use nom::character::complete::line_ending;
use nom::regexp::str::{re_find, re_match, re_matches, re_capture};
use nom::multi::many0;
struct Entry {
title: String,
body: String,
}
fn get_entry_title(i: &str) -> IResult<&str, &str> {
delimited(tag(""),
take_until(alt((
tag("\r\n"),
tag("\n")
))),
alt((
tag("\r\n"),
tag("\n")
))
)(i)
}
fn get_entry_body(i: &str) -> IResult<&str, &str> {
terminated(
take_until( tag("sep/")),
tag("sep/")
)(i)
}
fn main() {
let data = r#"title1
title1 line1
title1 line2
sep/
title2
title2 line1
title2 line2
title2 line3
sep/
title3
title3 line1
sep/"#;
let result = get_entry_title(&data);
}