java.util.regex.Matcher Scala Examples

The following examples show how to use java.util.regex.Matcher. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
Example 1
Source File: CodeFormatter.scala    From drizzle-spark   with Apache License 2.0 5 votes vote down vote up
package org.apache.spark.sql.catalyst.expressions.codegen

import java.util.regex.Matcher

")) code.append(" ")
      code.append(line)
    }
    code.append("\n")
    indentLevel = newIndentLevel
    indentString = " " * (indentSize * newIndentLevel)
    currentLine += 1
  }

  private def result(): String = code.result()
} 
Example 2
Source File: CodeFormatter.scala    From XSQL   with Apache License 2.0 5 votes vote down vote up
package org.apache.spark.sql.catalyst.expressions.codegen

import java.util.regex.Matcher

")) code.append(" ")
      code.append(line)
    }
    code.append("\n")
    indentLevel = newIndentLevel
    indentString = " " * (indentSize * newIndentLevel)
    currentLine += 1
  }

  private def result(): String = code.result()
} 
Example 3
Source File: AkkaConfigPropertySourceAdapterPatternSpec.scala    From akka-spring-boot   with Apache License 2.0 5 votes vote down vote up
package com.github.scalaspring.akka.config

import java.util.regex.Matcher

import com.typesafe.scalalogging.StrictLogging
import org.scalatest.prop.TableDrivenPropertyChecks._
import org.scalatest.{FlatSpec, Matchers}

class AkkaConfigPropertySourceAdapterPatternSpec extends FlatSpec with Matchers with StrictLogging {

  val indexed = Table(
    ("name",                      "path",                 "index"),
    ("x[0]",                      "x",                    0),
    ("someProperty[0]",           "someProperty",         0),
    ("some_property[1]",          "some_property",        1),
    ("some.property[0]",          "some.property",        0),
    (" some.property[0] ",        "some.property",        0),
    ("some.other.property[893]",  "some.other.property",  893)
  )

  val nonIndexed = Table(
    ("name"),
    ("x"),
    ("someProperty"),
    ("some_property"),
    ("some.property"),
    ("some.other.property")
  )

  "Indexed property regular expression" should "match indexed property names" in {
    forAll (indexed) { (name: String, path: String, index: Int) =>
      val m: Matcher = AkkaConfigPropertySourceAdapter.INDEXED_PROPERTY_PATTERN.matcher(name)
      m.matches() shouldBe true
      m.group("path") shouldEqual path
      m.group("index") shouldEqual index.toString
    }
  }

  it should "not match non-indexed property names" in {
    forAll (nonIndexed) { (name: String) =>
      val m: Matcher = AkkaConfigPropertySourceAdapter.INDEXED_PROPERTY_PATTERN.matcher(name)
      m.matches() shouldBe false
    }
  }

} 
Example 4
Source File: AssertPatternParsers.scala    From incubator-daffodil   with Apache License 2.0 5 votes vote down vote up
package org.apache.daffodil.processors.parsers

import java.util.regex.Matcher

import org.apache.daffodil.dsom.CompiledExpression
import org.apache.daffodil.dsom.SchemaDefinitionDiagnosticBase
import org.apache.daffodil.processors._
import org.apache.daffodil.util.OnStack
import org.apache.daffodil.schema.annotation.props.gen.FailureType

trait AssertMessageEvaluationMixin {
  def messageExpr: CompiledExpression[AnyRef]
  def discrim: Boolean

  def getAssertFailureMessage(state: PState): String = {
    val message =
      try {
        messageExpr.evaluate(state).asInstanceOf[String]
      } catch {
        // Note that we intentionally catch an SDE here. This effectively
        // converts an SDE (which is usually considered fatal) to a ParseError
        // (which isn't fatal). But section 7.3.1 of the DFDL specification
        // states that both types of errors should be caught and replaced with
        // a recoverable error and an implementation defined replacement
        // message should be provided instead.
        case e @ (_: ParseError | _: SchemaDefinitionDiagnosticBase) => {
          val kindString = if (discrim) "Discriminator" else "Assertion"
          kindString + " message expression evaluation failed: " + e.getMessage
        }
      }
    message
  }
}

class AssertPatternParser(
  override val context: TermRuntimeData,
  override val discrim: Boolean,
  testPattern: String,
  override val messageExpr: CompiledExpression[AnyRef],
  failureType: FailureType)
  extends PrimParser
  with AssertMessageEvaluationMixin {
  override lazy val runtimeDependencies = Vector()

  override def toBriefXML(depthLimit: Int = -1) = {
    val kindString = if (discrim) "Discriminator" else "Assertion"
    "<" + kindString + ">" + testPattern + "</" + kindString + ">"
  }

  lazy val pattern = ("(?s)" + testPattern).r.pattern // imagine a really big expensive pattern to compile.
  object withMatcher extends OnStack[Matcher](pattern.matcher(""))

  final def parse(start: PState): Unit = {
    val bytePos = (start.bitPos >> 3).toInt

    val dis = start.dataInputStream
    val mark = dis.markPos
    withMatcher { m =>
      val isMatch = dis.lookingAt(m, start)
      if (!isMatch) {
        val message = getAssertFailureMessage(start)
        if (failureType == FailureType.ProcessingError) {
          val diag = new AssertionFailed(context.schemaFileLocation, start, message)
          start.setFailed(diag)
        } else
          start.SDW(message)
      } else if (discrim) {
        // Only want to set the discriminator if there was a match.
        start.setDiscriminator(true)
      }
    }
    dis.resetPos(mark)
  }
} 
Example 5
Source File: OnStack.scala    From incubator-daffodil   with Apache License 2.0 5 votes vote down vote up
package org.apache.daffodil.util

import scala.collection.mutable
import java.util.regex.Matcher

sealed abstract class LocalStackBase[T](
    constructorFunc: => T,
    optionalResetFunc: (T => Unit)) {

  protected def stack: mutable.ArrayStack[T]

  
  val pattern = "ab*".r.pattern // imagine a really big expensive pattern to compile.

  object withMatcher extends OnStack[Matcher](pattern.matcher(""))

  def foo(str: String): Unit = {
    withMatcher { m =>
      //
      // we reset it here in our own code
      // because in case of a matcher, the reset needs an argument
      //
      m.reset(str)

      while (m.find()) {
        m.regionStart() // and so on
      }
    }
  }
} 
Example 6
Source File: OS.scala    From uap-scala   with Do What The F*ck You Want To Public License 5 votes vote down vote up
package org.uaparser.scala

import MatcherOps._
import java.util.regex.{ Matcher, Pattern }
import scala.util.control.Exception.allCatch

case class OS(family: String, major: Option[String] = None, minor: Option[String] = None, patch: Option[String] = None,
              patchMinor: Option[String] = None)

object OS {
  private[scala] def fromMap(m: Map[String, String]) = m.get("family").map { family =>
    OS(family, m.get("major"), m.get("minor"), m.get("patch"), m.get("patch_minor"))
  }

  private[this] val quotedBack1: Pattern = Pattern.compile(s"(${Pattern.quote("$1")})")

  private[this] def replacementBack1(matcher: Matcher)(replacement: String): String =
    if (matcher.groupCount() >= 1) {
      quotedBack1.matcher(replacement).replaceAll(matcher.group(1))
    } else replacement

  private[this] def replaceBackreference(matcher: Matcher)(replacement: String): Option[String] =
    getBackreferenceGroup(replacement) match {
      case Some(group) => matcher.groupAt(group)
      case None        => Some(replacement)
    }

  private[this] def getBackreferenceGroup(replacement: String): Option[Int] =
    for {
      ref <- Option(replacement).filter(_.contains("$"))
      groupOpt = allCatch opt ref.substring(1).toInt
      group <- groupOpt
    } yield group


  private[scala] case class OSPattern(
    pattern: Pattern,
    osReplacement: Option[String],
    v1Replacement: Option[String],
    v2Replacement: Option[String],
    v3Replacement: Option[String],
    v4Replacement: Option[String]
  ) {
    def process(agent: String): Option[OS] = {
      val matcher = pattern.matcher(agent)
      if (!matcher.find()) None else {
        osReplacement
          .map(replacementBack1(matcher))
          .orElse(matcher.groupAt(1)).map { family =>
            val major = v1Replacement.flatMap(replaceBackreference(matcher)).orElse(matcher.groupAt(2))
            val minor = v2Replacement.flatMap(replaceBackreference(matcher)).orElse(matcher.groupAt(3))
            val patch = v3Replacement.flatMap(replaceBackreference(matcher)).orElse(matcher.groupAt(4))
            val patchMinor = v4Replacement.flatMap(replaceBackreference(matcher)).orElse(matcher.groupAt(5))
            OS(family, major, minor, patch, patchMinor)
        }
      }
    }
  }

  private object OSPattern {
    def fromMap(m: Map[String, String]): Option[OSPattern] = m.get("regex").map { r =>
      OSPattern(Pattern.compile(r), m.get("os_replacement"), m.get("os_v1_replacement"), m.get("os_v2_replacement"),
        m.get("os_v3_replacement"), m.get("os_v4_replacement"))
    }
  }

  case class OSParser(patterns: List[OSPattern]) {
    def parse(agent: String): OS = patterns.foldLeft[Option[OS]](None) {
      case (None, pattern) => pattern.process(agent)
      case (result, _) => result
    }.getOrElse(OS("Other"))
  }

  object OSParser {
    def fromList(config: List[Map[String, String]]): OSParser = OSParser(config.map(OSPattern.fromMap).flatten)
  }
} 
Example 7
Source File: UserAgent.scala    From uap-scala   with Do What The F*ck You Want To Public License 5 votes vote down vote up
package org.uaparser.scala

import MatcherOps._
import java.util.regex.{Matcher, Pattern}

case class UserAgent(family: String, major: Option[String] = None, minor: Option[String] = None,
                     patch: Option[String] = None)

object UserAgent {
  private[scala] def fromMap(m: Map[String, String]) = m.get("family").map { family =>
    UserAgent(family, m.get("major"), m.get("minor"), m.get("patch"))
  }

  private[scala] case class UserAgentPattern(pattern: Pattern, familyReplacement: Option[String],
                                      v1Replacement: Option[String], v2Replacement: Option[String],
                                      v3Replacement: Option[String]) {
    def process(agent: String): Option[UserAgent] = {
      val matcher = pattern.matcher(agent)
      if (!matcher.find()) return None
      familyReplacement.map { replacement =>
        if (replacement.contains("$1") && matcher.groupCount() >= 1) {
          replacement.replaceFirst("\\$1", Matcher.quoteReplacement(matcher.group(1)))
        } else replacement
      }.orElse(matcher.groupAt(1)).map { family =>
        val major = v1Replacement.orElse(matcher.groupAt(2)).filter(_.nonEmpty)
        val minor = v2Replacement.orElse(matcher.groupAt(3)).filter(_.nonEmpty)
        val patch = v3Replacement.orElse(matcher.groupAt(4)).filter(_.nonEmpty)
        UserAgent(family, major, minor, patch)
      }
    }
  }

  private object UserAgentPattern {
    def fromMap(config: Map[String, String]): Option[UserAgentPattern] = config.get("regex").map { r =>
      UserAgentPattern(Pattern.compile(r), config.get("family_replacement"), config.get("v1_replacement"),
        config.get("v2_replacement"), config.get("v3_replacement"))
    }
  }

  case class UserAgentParser(patterns: List[UserAgentPattern]) {
    def parse(agent: String): UserAgent = patterns.foldLeft[Option[UserAgent]](None) {
      case (None, pattern) => pattern.process(agent)
      case (result, _) => result
    }.getOrElse(UserAgent("Other"))
  }

  object UserAgentParser {
    def fromList(config: List[Map[String, String]]): UserAgentParser =
      UserAgentParser(config.map(UserAgentPattern.fromMap).flatten)
  }
} 
Example 8
Source File: Device.scala    From uap-scala   with Do What The F*ck You Want To Public License 5 votes vote down vote up
package org.uaparser.scala

import java.util.regex.{ Matcher, Pattern }
import org.uaparser.scala.MatcherOps._

case class Device(family: String, brand: Option[String] = None, model: Option[String] = None)

object Device {
  private[scala] def fromMap(m: Map[String, String]) = m.get("family").map(Device(_, m.get("brand"), m.get("model")))

  private[scala] case class DevicePattern(pattern: Pattern, familyReplacement: Option[String],
                                           brandReplacement: Option[String], modelReplacement: Option[String]) {
    def process(agent: String): Option[Device] = {
      val matcher = pattern.matcher(agent)
      if (!matcher.find()) None else {
        val family = familyReplacement.map(r => replace(r, matcher)).orElse(matcher.groupAt(1))
        val brand = brandReplacement.map(r => replace(r, matcher)).filterNot(s => s.isEmpty)
        val model = modelReplacement.map(r => replace(r, matcher)).orElse(matcher.groupAt(1)).filterNot(s => s.isEmpty)
        family.map(Device(_, brand, model))
      }
    }

    def replace(replacement: String, matcher: Matcher): String = {
      (if (replacement.contains("$") && matcher.groupCount() >= 1)  {
        (1 to matcher.groupCount()).foldLeft(replacement)((rep, i) => {
          val toInsert = if (matcher.group(i) ne null) matcher.group(i) else ""
          rep.replaceFirst("\\$" + i, Matcher.quoteReplacement(toInsert))
        })
      } else replacement).trim
    }
  }

  private object DevicePattern {
    def fromMap(m: Map[String, String]): Option[DevicePattern] = m.get("regex").map { r =>
      val pattern = m.get("regex_flag").map(flag =>
        Pattern.compile(r, Pattern.CASE_INSENSITIVE)).getOrElse(Pattern.compile(r)
      )
      DevicePattern(pattern, m.get("device_replacement"), m.get("brand_replacement"), m.get("model_replacement"))
    }
  }

  case class DeviceParser(patterns: List[DevicePattern]) {
    def parse(agent: String): Device = patterns.foldLeft[Option[Device]](None) {
      case (None, pattern) => pattern.process(agent)
      case (result, _) => result
    }.getOrElse(Device("Other"))
  }

  object DeviceParser {
    def fromList(config: List[Map[String, String]]): DeviceParser =
      DeviceParser(config.map(DevicePattern.fromMap).flatten)
  }
} 
Example 9
Source File: StringUtil.scala    From ingraph   with Eclipse Public License 1.0 5 votes vote down vote up
package ingraph.compiler.cypher2gplan.util

import java.io.UnsupportedEncodingException
import java.util.regex.{Matcher, Pattern}
import javax.xml.bind.DatatypeConverter

import org.apache.spark.sql.catalyst.{expressions => cExpr}
import org.slizaa.neo4j.opencypher.{openCypher => oc}

object StringUtil {
  private val patternStringDelimiterCheck = Pattern.compile("^'.*'$|^\".*\"$")
  private val patternStringDelimiterReplace = Pattern.compile("^[\"']|[\"']$")
  // note: literal \ should be escaped twice: first for the regular expression syntax
  // and then for the Java String in the source code, so \\\\ below matches the literal backslash
  private val patterBackslashNotation = Pattern.compile(
  "(?<!\\\\)\\\\(\\\\|'|\"|b|f|n|r|t|_|%|u([0-9a-fA-F]{4})|U([0-9a-fA-F]{8}))"
  )

  
  def toOptionInt(s: String): Option[Int] = {
    Option(s).filterNot( _.isEmpty ).map( _.toInt )
  }
}