efficient regex in scala
The super easy way to match RegEx in Scala is to use the matches
method that is part of the string class.
def matches
…is added by an implicit conversion from StringOps to String. Where needed, Strings are implicitly converted into instances of StringOps (indexed sequences)
For a more performant variety, lets consider this statement from the Scala Docs:
An instance of Regex represents a compiled regular expression pattern. Since compilation is expensive, frequently used Regexes should be constructed once, outside of loops and perhaps in a companion object. The canonical way to create a Regex is by using the method
r
, provided implicitly for strings
Once we construct a RegEx, strings can be tested using the standard Scala match
expression: