Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RegexOps #595

Merged
merged 1 commit into from
Jun 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"))
}

}