Scala Regex: how to match all occurrences of a multiline regex pattern

Viewed 27

I'm trying to match and collect all the markdown style link references from a given string. I have the following

  val str = """
[1] hello
[2] world
some text with [a][2]

[3] bye bye
  """

  val regex = """^ {0,3}(?:(\[[^\]]+\])(.+))$""".r

  for m <- regex.findAllMatchIn(str) do 
    println(m.toString())

But it doesn't return any matches. If I instead use this regex """ {0,3}(?:(\[[^\]]+\])(.+))""".r then it ends up matching " [a][2]" as well, which I don't want to match. How do I solve this problem?

0 Answers
Related