Reputation: 11512
I'd like to have something that matches basically anything to end-of-line of a multi-line string. Ideally something like this:
def almostAnything: Parser[String] = """[^\r\n]+""".r ^^ { _.toString.trim }
Problem is... this doesn't work. It ignores the negated \n and just keeps matching away onto the next chunk of string after the \n.
Why is that, and how can I successfully match anything up to end-of-line (!= end-of-string)?
Upvotes: 0
Views: 181
Reputation: 6168
I'm not sure how the parsers work, but the problem you're encountering here is not with your regular expression, but with the way Regex
works.
By defaults, Regex
s are anchored - they expect to match the entire string. If you want partial matches (and it seems that you do), you must unanchor them.
For example:
val sameLine = """([^\r\n]+)""".r.unanchored
"45ft\n something" match {
case sameLine(c) => Some(c)
case _ => None
}
This yields Some(45ft)
.
Upvotes: 1