I wrote the following code
val input = """{"user":"foo", "name":"bar"}"""
val regex = """.*(?<="user":)"(.*)".*""".r
regex.findAllIn(input).matchData map{m => m.group(1)} toList
The output is res5: List[String] = List("foo\", \"name\":\"bar")
Now I can easily change my regex to
val regex = """.*(?<="user":)"([^"]+)".*""".r
That's not the point...
But my point is why did the .*
grab the entire string? My understanding was that the Regex engine is greedy generous
so the regex should have seen that after the (.*)
there is a "
so the generous part should have kicked in and the regex should have only matched the string foo
but it seems that the generous part did not kick in.