import com.github.verbalexpressions.VerbalExpression
import VerbalExpression._
val validUrl = $.startOfLine()
.andThen("http")
.maybe("s")
.andThen("://")
.maybe("www.")
.anythingBut(" ")
.endOfLine()
assert("https://www.google.com" is validUrl)
assert("ftp://home.comcast.net" isNot validUrl)
For more methods, checkout the wiki and the source
The intention of self-documenting code is to replace comments with self-evident code. For example:
BAD (have to mental-parse regex and/or trust the comment):
val numberRegex = """(\Q-\E)?\d+((\Q.\E)\d+)?""" // negative sign followed by digits, followed by optional fraction part
GOOD (code describes what it is and assert in end to make sure no magic was done by the library):
val fraction = $.andThen(".").digits()
val number = $.maybe("-").digits().maybe(fraction) // verbalexpressions can be composed
assert(number.regexp == numberRegex)
assert(Seq("3", "-4", "-0.458") forall number.check)
assert(Seq("0.", "hello", "4.3.2") forall number.notMatch)
Performance: As fast as vanilla regexes - the library is just a builder around a regex string. You can always access the underlying compiled Pattern:
val money = $.oneOf("$", "€", "₹", "¥").digits() // works with symbols too
val pattern: java.util.regex.Pattern = money.compile // the compiled pattern
Add the following to your build.sbt
:
resolvers += "Sonatype releases" at "http://oss.sonatype.org/content/repositories/releases/"
libraryDependencies += "com.github.verbalexpressions" %% "ScalaVerbalExpressions" % "1.0.1"