so I am creating an WML like language for my assignment and as a first step, I am supposed to create regular expressions to recognize the following:
//single = "{"
//double = "{{"
//triple = "{{{"
here is my code for the second one:
val double = "\\{\\{\\b".r
and my Test is:
println(double.findAllIn("{{ s{{ { {{{ {{ {{x").toArray.mkString(" "))
Bit it doesn't print anything ! It's supposed to print the first, second, fifth and 6th token. I have tried every single combination of \b and \B and even \{{2,2} instead of \{\{ but it's still not working. Any help??
As a side question, If I wanted it to match just the first and fifth tokens, what would I need to do?
I tested your code (Scala 2.12.2 REPL), and in contrary to your "it doesn't print anything" statement, it actually prints "{{" occurrence from "{{x" substring.
This is because
xis a word character and\bmatches a position between second{andx. Keep in mind that{isn't a word character, unlikex.As per this tutorial
As for solution, it depends on precise definition, but lookarounds seemed to work for me:
It matched "first, second, fifth and 6th token". The expression says match "{{" not preceded and not followed by "{".
For side-question:
Or, depending on your interpretation of "space":
matched 1st and 5th tokens. I couldn't use positive lookarounds coz I wanted to take line beginnings and endings (boundaries) into account automatically. So double negation by
!and[^ ]created an effect of implicit inclusion of^and$. Alternatively, you could use:You can read about lookarounds here. Basically they match the symbol or expression as boundary; simply saying they match stuff but don't include it in the matched string itself.
Some examples of lookarounds
(?<=z)aaamatches "aaa" that is preceded byz(?<!z)aaamatches "aaa" that is not preceded byzaaa(?=z)matches "aaa" followed byzaaa(?!z)matches "aaa" not followed byzP.S. Just to make your life easier, Scala has
"""for escaping, so let's say instead of:you can just: