watzon
watzon

Reputation: 2549

Regex to match everything between

I've been trying at this for an hour, but I'm no regexpert. What I want to do seems fairly simple, but it's turning out a lot more difficult than I would have thought.

Basically I have this:

<<< Some code

    def prnt(string)
        print(string)
    end

=====

    def println(string)
        puts(string)
    end

*****
<<< Some more code
...

What I want to do is capture everything between the first line <<< Some code and the *****. There will be lots of blocks like this in a file.

The regex that I have so far is this (?:<<< .*\r?\n)([\s\S]+)(?:[*]{5}), but it doesn't really work. Any ideas? The language I'm using it in is Go.

Upvotes: 0

Views: 71

Answers (2)

Bohemian
Bohemian

Reputation: 425043

This captures the target in group 1, but you must use the "dot matches newline" switch "s":

<<<[^\r\n]+[\r\n]*(.*?)[*]{5}

See live demo.

Upvotes: 0

watzon
watzon

Reputation: 2549

Never mind I figured it out!

(?:<<< .*\r?\n)([\s\S]*?)(?:[*]{5})

It seems like the big thing was making the match group in the middle lazy so that it would match as little as possible.

Upvotes: 2

Related Questions