Scala regexes + stripMargin

Sasha Solomon
code post-its
Published in
1 min readFeb 8, 2017

I was working with some reaaallllyyy long regexes in Scala.

Something like:

(^(https://example\.com/ex/)|(^/ex/)|(^example\.com/ex/)|(^http://localhost)|(^http://example\.dev)|example\.ex\.com|\.com/Example|example\.co)(?:.+)

The actual regex doesn’t really matter. The point is, it’s really long. So, I wanted to write it on multiple lines instead of one, really long line.

In Scala you can do this with """ """.stripMargin. So something like:

"""Super long string that I want to be on 
|multiple lines because it is so long and
|it would be easier to read this way
|""".stripMargin

This turns your long String into a multiline String and then removes all instances of the multiline character (by default is |) and the “margin” which is the tabbed in portion.

You might guess that if you use this with a regex, you’ll run into problems with the default | . And you’re right! stripMargin will strip out all occurrences of the |.

However, you can choose your own multiline character. So something like:

"""(^(https://example\.com/ex/)|(^/ex/)|
~(^example\.com/ex/)|(^http://localhost)|
~(^http://example\.dev)|example\.ex\.com|
~\.com/Example|example\.co)(?:.+)
~""".stripMargin('~')

Now here comes the gotcha. stripMargin does not strip the \n character, only the margin.

This means that our regex will include newlines and probably won’t work.

We can fix this by also stripping out the \n characters.

"""(^(https://example\.com/ex/)|(^/ex/)|
~(^example\.com/ex/)|(^http://localhost)|
~(^http://example\.dev)|example\.ex\.com|
~\.com/Example|example\.co)(?:.+)
~""".stripMargin('~').replaceAll("\n", "").r

We use replaceAll and remove all newlines. Add the .r at the end to turn it into a regex and we’re done!

--

--

Sasha Solomon
code post-its

software engineer @twitter, previously @medium. doing scala + graphql. pokemon gym leader. potato compatible. @sachee