Skip to content

Commit

Permalink
Add RegexOps
Browse files Browse the repository at this point in the history
  • Loading branch information
jozic committed May 28, 2023
1 parent 644445f commit 956249b
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 1 deletion.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ lazy val compat = new MultiScalaCrossProject(
sharedSourceDir / "scala-2.11_2.12"
}
},
versionPolicyIntention := Compatibility.BinaryAndSourceCompatible,
versionPolicyIntention := Compatibility.BinaryCompatible,
mimaBinaryIssueFilters ++= {
import com.typesafe.tools.mima.core._
import com.typesafe.tools.mima.core.ProblemFilters._
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Scala (https://www.scala-lang.org)
*
* Copyright EPFL and Lightbend, Inc.
*
* Licensed under Apache License 2.0
* (http://www.apache.org/licenses/LICENSE-2.0).
*
* See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*/

package scala.util.matching

package object compat {
final implicit class RegexOps(val regex: Regex) extends AnyVal {

/** Returns whether this `Regex` matches the given character sequence.
*
* Like the extractor, this method takes anchoring into account.
*
* @param source The text to match against
* @return true if and only if `source` matches this `Regex`.
* @see [[Regex#unanchored]]
* @example {{{"""\d+""".r matches "123" // returns true}}}
*/
def matches(source: CharSequence): Boolean = regex.pattern.matcher(source).matches()
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Scala (https://www.scala-lang.org)
*
* Copyright EPFL and Lightbend, Inc.
*
* Licensed under Apache License 2.0
* (http://www.apache.org/licenses/LICENSE-2.0).
*
* See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*/

package scala.util.matching

package object compat {
type Regex = scala.util.matching.Regex
val Regex = scala.util.matching.Regex
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Scala (https://www.scala-lang.org)
*
* Copyright EPFL and Lightbend, Inc.
*
* Licensed under Apache License 2.0
* (http://www.apache.org/licenses/LICENSE-2.0).
*
* See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*/

package scala.util.matching.compat

import org.junit.Assert._
import org.junit.Test

class RegexOpsTest {

@Test
def testMatches(): Unit = {
assertTrue(".*hello.*".r.matches("hey hello"))
assertFalse(".*hello.*".r.matches("hey hop"))
}

}

0 comments on commit 956249b

Please sign in to comment.