Here is the code:
val s =
"""
|\begin{bmatrix}
|\cos \theta & -\sin \theta \\
|\sin \theta & \cos \alpha
|\end{bmatrix}
""".stripMargin
val lastWordPattern = """(?s)(.*)\s+(.*)""".r
def wordToPos(string: String, position: Int): String = {
val subString = string.substring(0, position)
println("sub: ", subString)
subString match {
case lastWordPattern(x@_*) => {
println(0, x(0))
println(1, x(1))
x(0)
}
case _ => ""
}
}
wordToPos(s, 20)
Result from sbt console:
scala> wordToPos(s, 20)
(sub: ,
\begin{bmatrix}
\co)
(0,
\begin{bmatrix})
(1,\co)
Result from chrome:
(sub: ,
\begin{bmatrix}
\co)
In chrome the code prints the substring, but fails to match the regex. Am I doing something wrong?
(?s)modifier. Try replacing all.with[\s\S]and retry.