Closed green-coder closed 5 years ago
In general, I've never used #"\Z". I don't offhand see how it would be useful, since instaparse is always going to try to match against the whole string anyway.
:
But if that's what you want to do, I think you want to make the Z
a lower-case z
.
https://stackoverflow.com/questions/2707870/whats-the-difference-between-z-and-z-in-a-regular-expression-and-when-and-how
The upper-case one matches both before and after the final newline character. That's Java (and therefore Clojure) behavior:
> (re-seq #"\Z" "\n")
("" "")
Some other options available to you are:
Paragraph = NonBlankLine+ BlankLine+ EOF
As for the negative lookahead example, there's nothing in your grammar to force that it must end with an EOF, so the parse it produced is perfectly valid. Also, watch out: in Java/Clojure the default behavior of #"." is that the . isn't matched by newline characters.
I found a strange behavior with
#'\\Z'
, I wonder if it is a bug.This other approach which uses the negative lookahead does put the
"\n"
in the right place in the result, but there is another problem: TheBlankLine
is missing in the result. That may be a bug of instaparse.I am using the version
1.4.9
of instaparse.