简体   繁体   中英

How can I parse until a separator is found or the end of the input is reached with Sprache?

I'm trying to parse a string that contains text interspersed with stars:

var input = "*This is the first part*This is the second part";

I want to extract any text between stars and the text after the last star. The string doesn't end in a star, or a newline.

I've written a parser with Sprache to try and achieve this:

Parser<string> thingAfterStarParser = (
    from open in Parse.String("*")
    from rest in Parse.AnyChar.Many().Text()
    select rest
);

var result = thingAfterStarParser.AtLeastOnce().Parse(input);

But result only ends up with one element, instead of two. I think it's because rest is parsing all the way to the end of the input.

How can I tell the parser to parse until stars or the end of the input? Any help would be much appreciated! Thank you

I think it's because rest is parsing all the way to the end of the input.

You're right about that. Parse.AnyChar will not stop at next *. You can do it like this:

public class UnitTest1
{
    readonly ITestOutputHelper output;

    public UnitTest1(ITestOutputHelper output) => this.output = output;

    [Fact]
    public void Split()
    {
        const string input = "*This is the first part*This is the second part";

        var thingAfterStarParser = 
            from open in Parse.String("*")
            from rest in Parse.AnyChar.Except(Parse.Char('*')).Many().Text()
            select rest;

        var result = thingAfterStarParser.AtLeastOnce().Parse(input);

        foreach (var message in result)
        {
            output.WriteLine(message);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM