Parsing strings using Nom

How can I parse this string?

let mystring = r#""world\" is \" not big enough""#

I was trying something like this, but it goes into infinite recursion

pub fn parse_string(input:&str)->IResult<&str, &str>{
    delimited(char('"'), 
    escaped(take_until("\""),'\\', one_of(r#""n\tb"#)), 
    char('"'))(input)
}

Your take_until will try to parse all up to ", including \. But the docs for escaped state that The first argument matches the normal characters (it must not accept the control character). So I guess it should not match the \. So I tried take_until("\"\\") instead, but this doesn't mean “take until " or \”, but searches for "\ literal.

So I've used none_of instead:

    delimited(
        char('"'),
        escaped(none_of("\"\\"), '\\', one_of(r#""n\tb"#)),
        char('"'),
    )(input)
5 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.