本文整理汇总了Scala中java.util.regex.Pattern类的典型用法代码示例。如果您正苦于以下问题:Scala Pattern类的具体用法?Scala Pattern怎么用?Scala Pattern使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Pattern类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: GenerateSCSSAction
//设置package包名称以及导入依赖的类
package com.xcodersteam.idea.plugins.mkupscss
import java.awt.datatransfer.StringSelection
import java.util.regex.Pattern
import com.intellij.openapi.actionSystem.{AnAction, AnActionEvent, CommonDataKeys}
import com.intellij.openapi.ide.CopyPasteManager
import com.intellij.openapi.ui.popup.JBPopupFactory
import com.intellij.openapi.util.text.StringUtil
import com.intellij.openapi.wm.WindowManager
class GenerateSCSSAction extends AnAction {
val pattern = Pattern.compile("<[^>]*>")
override def update(e: AnActionEvent): Unit = {
val project = e.getData(CommonDataKeys.PROJECT)
val editor = e.getData(CommonDataKeys.EDITOR)
val lang = e.getData(CommonDataKeys.PSI_FILE).getFileType.getName
e.getPresentation.setVisible(CodeLangHelper.langs.get(lang).isDefined && project != null && editor != null)
e.getPresentation.setEnabled(editor.getSelectionModel.hasSelection() && pattern.matcher(editor.getSelectionModel.getSelectedText).find())
}
override def actionPerformed(anActionEvent: AnActionEvent): Unit = {
val editor = anActionEvent.getData(CommonDataKeys.EDITOR)
val lang = anActionEvent.getData(CommonDataKeys.PSI_FILE).getFileType.getName
val text = editor.getSelectionModel.getSelectedText()
val statusBar = WindowManager.getInstance().getStatusBar(editor.getProject)
val codeLang = CodeLangHelper.langs.get(lang).orNull
val prefix = "[" + codeLang.name + "->SCSS]"
try {
if (codeLang == null)
throw new CodeConvertingException(message = "Failed to recognize file type")
val generated = CodeConverter(codeLang.prepareText(text), codeLang)
CopyPasteManager.getInstance().setContents(new StringSelection(StringUtil.convertLineSeparators(generated, "\n")))
JBPopupFactory.getInstance()
.createHtmlTextBalloonBuilder(prefix + " Successful generated scss and copied to clipboard", com.intellij.openapi.ui.MessageType.INFO, null)
.setFadeoutTime(5000)
.createBalloon().showInCenterOf(statusBar.getComponent)
} catch {
case e: CodeConvertingException =>
JBPopupFactory.getInstance()
.createHtmlTextBalloonBuilder(e.errorMessage, com.intellij.openapi.ui.MessageType.ERROR, null)
.setFadeoutTime(5000)
.createBalloon().showInCenterOf(statusBar.getComponent)
}
}
}
示例2: PScoutTranslator
//设置package包名称以及导入依赖的类
package org.argus.amandroid.core.util
import org.argus.jawa.core.util._
import java.util.regex.Pattern
import org.argus.jawa.core.{JavaKnowledge, Signature}
object PScoutTranslator {
def main(args: Array[String]): Unit = {
val filepath = args(0)
val fileuri = FileUtil.toUri(filepath)
translate(fileuri)
}
def translate(uri: FileResourceUri): IMap[String, ISet[Signature]] = {
val permissionMap: MMap[String, MSet[Signature]] = mmapEmpty
var currentPermission: String = null
scala.io.Source.fromFile(FileUtil.toFile(uri)).getLines().foreach {
case permission if permission.startsWith("Permission:") =>
currentPermission = permission.replace("Permission:", "")
case sigstr if sigstr.startsWith("<") =>
val sig = formatSignature(sigstr)
permissionMap.getOrElseUpdate(currentPermission, msetEmpty) ++= sig
case _ =>
}
permissionMap.map{case (k, v) => (k, v.toSet)}.toMap
}
// 1 2 3 4
private val regex = "<([[^\\s]&&[^:]]+):\\s([^\\s]+)\\s([[^\\s]&&[^\\(]]+)\\(([[^\\s]&&[^\\)]]*)\\)>\\s+\\(.*\\)"
private def formatSignature(sigstr: String): Option[Signature] = {
val p: Pattern = Pattern.compile(regex)
val m = p.matcher(sigstr)
if(m.find()){
val classTypStr = m.group(1)
val retTypStr = m.group(2)
val methodName = m.group(3)
val paramTypStrList = m.group(4).split(",")
val classTyp = JavaKnowledge.getTypeFromJawaName(classTypStr)
val protosb = new StringBuilder
protosb.append("(")
paramTypStrList.foreach{
paramTypStr =>
if(!paramTypStr.isEmpty)
protosb.append(JavaKnowledge.formatTypeToSignature(JavaKnowledge.getTypeFromJawaName(paramTypStr)))
}
protosb.append(")")
protosb.append(JavaKnowledge.formatTypeToSignature(JavaKnowledge.getTypeFromJawaName(retTypStr)))
Some(Signature(classTyp, methodName, protosb.toString()))
} else {
System.err.println("PScoutTranslator, does not match: " + sigstr)
None
}
}
}
示例3: URLInString
//设置package包名称以及导入依赖的类
package org.argus.jawa.core.util
import java.util.regex.Pattern
object URLInString {
def extract(str: String): Set[String] = {
val results = msetEmpty[String]
val regex = "\\b(((ht|f)tp(s?)\\:\\/\\/|~\\/|\\/)|www.)" +
"(\\w+:\\[email protected])?(([-\\w]+\\.)+(com|org|net|gov" +
"|mil|biz|info|mobi|name|aero|jobs|museum" +
"|travel|[a-z]{2}))(:[\\d]{1,5})?" +
"(((\\/([-\\w~!$+|.,=]|%[a-f\\d]{2})+)+|\\/)+|\\?|#)?" +
"((\\?([-\\w~!$+|.,*:]|%[a-f\\d{2}])+=?" +
"([-\\w~!$+|.,*:=]|%[a-f\\d]{2})*)" +
"(&(?:[-\\w~!$+|.,*:]|%[a-f\\d{2}])+=?" +
"([-\\w~!$+|.,*:=]|%[a-f\\d]{2})*)*)*" +
"(#([-\\w~!$+|.,*:=]|%[a-f\\d]{2})*)?\\b"
val p = Pattern.compile(regex)
val m = p.matcher(str)
while(m.find()) {
var urlStr = m.group()
if (urlStr.startsWith("(") && urlStr.endsWith(")"))
{
urlStr = urlStr.substring(1, urlStr.length() - 1)
}
results.add(urlStr)
}
results.toSet
}
}
示例4: StringUtils
//设置package包名称以及导入依赖的类
package utils
import java.text.Normalizer
import java.util.regex.Pattern
import java.security.SecureRandom
import scala.util.Try
object StringUtils {
def generateUuid(): String = {
java.util.UUID.randomUUID().toString
}
def deAccent(str: String): String = {
if (str == null || str.isEmpty) {
""
} else {
val nfdNormalizedString: String = Normalizer.normalize(str, Normalizer.Form.NFD);
val pattern: Pattern = Pattern.compile("\\p{InCombiningDiacriticalMarks}+");
pattern.matcher(nfdNormalizedString).replaceAll("");
}
}
//Random Generator
private[this] val random = new SecureRandom()
// Generate a random string of length n from the given alphabet
private[this] def randomString(alphabet: String)(n: Int): String = {
Stream.continually(random.nextInt(alphabet.size)).map(alphabet).take(n).mkString
}
// Generate a random alphabnumeric string of length n
def randomAlphanumericString(n: Int): String = {
randomString("abcdefghijklmnopqrstuvwxyz0123456789")(n)
}
}
示例5: TweetCollect
//设置package包名称以及导入依赖的类
package info.matsumana.flink
import java.util.regex.Pattern
import java.util.{HashMap, Properties}
import com.fasterxml.jackson.databind.ObjectMapper
import org.apache.flink.api.java.utils.ParameterTool
import org.apache.flink.streaming.api.scala._
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaProducer010
import org.apache.flink.streaming.connectors.twitter.TwitterSource
import org.apache.flink.streaming.util.serialization.SimpleStringSchema
object TweetCollect {
val DELETED_TWEET_PATTERN = Pattern.compile("""^\{"delete":\{""")
// ????
val TARGET_TWEET_PATTERN = Pattern.compile("^.*[\u3040-\u3096]+.*$")
val mapper = new ObjectMapper()
def main(args: Array[String]): Unit = {
val params = ParameterTool.fromPropertiesFile(args(0))
val env = StreamExecutionEnvironment.getExecutionEnvironment
env.enableCheckpointing(60000)
// source (twitter)
val twitterProps = new Properties()
twitterProps.setProperty(TwitterSource.CONSUMER_KEY, params.get("consumer_key", ""))
twitterProps.setProperty(TwitterSource.CONSUMER_SECRET, params.get("consumer_secret", ""))
twitterProps.setProperty(TwitterSource.TOKEN, params.get("token", ""))
twitterProps.setProperty(TwitterSource.TOKEN_SECRET, params.get("token_secret", ""))
val sourceStream = env.addSource(new TwitterSource(twitterProps))
// sink (kafka)
val kafkaProps = new Properties()
kafkaProps.setProperty("bootstrap.servers", params.get("bootstrap.servers", "localhost:9092"))
val topic = params.get("topic", "twitter")
val sink = new FlinkKafkaProducer010[String](
topic,
new SimpleStringSchema,
kafkaProps)
// stream processing
sourceStream
.filter(!DELETED_TWEET_PATTERN.matcher(_).matches())
.map(mapper.readValue(_, classOf[HashMap[String, Object]]))
.filter(m => {
val text = m.get("text")
text != null && TARGET_TWEET_PATTERN.matcher(String.valueOf(text)).matches()
})
.map(mapper.writeValueAsString(_))
.addSink(sink)
env.execute("TweetCollect")
}
}
示例6: getImageType
//设置package包名称以及导入依赖的类
package org.aj.awslambda
import java.awt.image.BufferedImage
import java.io.IOException
import java.net.URLDecoder
import com.amazonaws.services.lambda.runtime.events.S3Event
import java.util.regex.Pattern
import scala.concurrent.{Await, Future}
import scala.concurrent.ExecutionContext.Implicits.global
private def getImageType(srcKey: String): Option[String] = {
val matcher = Pattern.compile(".*\\.([^\\.]*)").matcher(srcKey)
matcher.matches() match {
case true => {
//get source image type and validate
val imageType = matcher.group(1)
imageTypes.get(imageType) match {
case Some(_) => Some(imageType)
case _ => None
}
}
case _ => None
}
}
private def decodeS3Key(key: String): String = URLDecoder.decode(key.replace("+", " "), "utf-8")
private def process(srcBucket: String, srcKey: String, imageType: String, dstBucket: String): Future[List[Url]] = {
//get source image with it's sizes
val sourceImage: (BufferedImage, Url) = getImage(srcBucket, srcKey)
//re-size
val original: Future[Url] = process(sourceImage._1, sourceImage._2, imageType, dstBucket, srcKey)
val resized: List[Option[Future[Url]]] = sizes.map { size =>
if (sourceImage._2.width != size) Some(process(sourceImage._1, size, imageType, dstBucket, srcKey))
else None
}
//return
Future.sequence(original :: resized.flatten)
}
}
示例7: RawCellContentExtractor
//设置package包名称以及导入依赖的类
package org.opencompare.io.wikipedia.parser
import java.util.regex.Pattern
import org.joda.time.DateTime
import org.sweble.wikitext.engine.PageTitle
import org.sweble.wikitext.engine.config.WikiConfig
import org.sweble.wikitext.parser.nodes._
import org.sweble.wom3.swcadapter.AstToWomConverter
import org.sweble.wom3.util.Wom3Toolbox
class RawCellContentExtractor(val wikiConfig : WikiConfig) {
private val trimPattern : Pattern = Pattern.compile("[\\s|!]*([\\s\\S]*?)\\s*")
private val nestedTableChecker : NestedTableChecker = new NestedTableChecker
private val wtToStringConverter : WtToStringConverter = new WtToStringConverter(wikiConfig)
def extract(cell : WtNode) : String = {
if (nestedTableChecker.hasNestedTable(cell)) {
"" // FIXME : we do not support nested tables for now
} else {
val code = wtToStringConverter.convert(cell)
if (code.isDefined) {
trim(code.get)
} else {
""
}
}
}
def trim(s: String): String = {
val matcher = trimPattern.matcher(s)
if (matcher.matches() && matcher.groupCount() == 1) {
matcher.group(1)
} else {
""
}
}
}
示例8: RegexPatternInterpreter
//设置package包名称以及导入依赖的类
package org.opencompare.formalizer.interpreters
import java.util.regex.{Matcher, Pattern}
import org.opencompare.api.java.{Value, Feature, Product}
abstract class RegexPatternInterpreter(initValidHeaders : List[String],
regex : String,
initParameters : List[String],
initConfident : Boolean) extends PatternInterpreter(initValidHeaders, initParameters, initConfident) {
private val pattern : Pattern = Pattern.compile(regex, Pattern.UNICODE_CHARACTER_CLASS |
Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE | Pattern.DOTALL)
override def matchAndCreateValue(s: String, product: Product, feature: Feature): Option[Value] = {
val matcher = pattern.matcher(s)
if (matcher.matches()) {
createValue(s, matcher, parameters, product, feature)
} else {
None
}
}
def createValue(s : String, matcher : Matcher, parameters : List[String], product : Product, feature : Feature) : Option[Value]
}
示例9: SVNDirEntryExt
//设置package包名称以及导入依赖的类
package csw.services.config.server.commons
import java.util.regex.Pattern
import csw.services.config.api.models.FileType
import org.tmatesoft.svn.core.{SVNDirEntry, SVNNodeKind}
object SVNDirEntryExt {
implicit class RichSvnDirEntry(val entry: SVNDirEntry) extends AnyVal {
def isFile: Boolean = entry.getKind == SVNNodeKind.FILE
def isNotActiveFile(activeFileName: String): Boolean = !entry.getName.endsWith(activeFileName)
def stripAnnexSuffix(annexSuffix: String): Unit =
entry.setRelativePath(entry.getRelativePath.stripSuffix(annexSuffix))
def matches(maybePattern: Option[Pattern]): Boolean = maybePattern match {
case None ? true
case Some(pattern) ? pattern.matcher(entry.getRelativePath).matches()
}
def matchesFileType(maybeFileType: Option[FileType], annexSuffix: String): Boolean = maybeFileType match {
case None ? true
case Some(FileType.Annex) ? isAnnex(annexSuffix)
case Some(FileType.Normal) ? !isAnnex(annexSuffix)
}
private def isAnnex(annexSuffix: String): Boolean = entry.getName.endsWith(annexSuffix)
}
}
示例10: message
//设置package包名称以及导入依赖的类
package com.wunder.pets.validations
import java.util.regex.{Matcher, Pattern}
import org.postgresql.util.PSQLException
trait ValidationError {
def message: String
}
final class IsEmpty(val field: String) extends ValidationError {
override def message: String = s"$field cannot be empty"
}
final class NotGreaterThan[T](val field: String, val lowerBound: T) extends ValidationError {
def message: String = s"$field must be greater than $lowerBound"
}
final class NotLessThan[T](val field: String, val upperBound: T) extends ValidationError {
def message: String = s"$field must be less than $upperBound"
}
final class DuplicateValue(val e: PSQLException) extends ValidationError {
override def message: String = {
val regex = "Key \\((.*)\\)=\\((.*)\\) already exists."
val m: Matcher = Pattern.compile(regex).matcher(e.getServerErrorMessage.getDetail);
if (m.matches) {
s"${m.group(1)} has a duplicate value of ${m.group(2)}"
} else {
"Could not determine field and value."
}
}
}
final class GeneralError(val message: String) extends ValidationError
示例11: KStreamBuilderS
//设置package包名称以及导入依赖的类
package com.github.aseigneurin.kafka.streams.scala
import java.util.regex.Pattern
import com.github.aseigneurin.kafka.streams.scala.ImplicitConversions._
import org.apache.kafka.common.serialization.Serde
import org.apache.kafka.streams.kstream.{GlobalKTable, KStreamBuilder}
import org.apache.kafka.streams.processor.TopologyBuilder
object KStreamBuilderS {
val inner = new KStreamBuilder
def stream[K, V](topics: String*)
(implicit keySerde: Serde[K], valSerde: Serde[V]): KStreamS[K, V] =
inner.stream[K, V](keySerde, valSerde, topics: _*)
def stream[K, V](offsetReset: TopologyBuilder.AutoOffsetReset,
topics: String*)
(implicit keySerde: Serde[K], valSerde: Serde[V]): KStreamS[K, V] =
inner.stream[K, V](offsetReset, keySerde, valSerde, topics: _*)
def stream[K, V](topicPattern: Pattern)
(implicit keySerde: Serde[K], valSerde: Serde[V]): KStreamS[K, V] =
inner.stream[K, V](keySerde, valSerde, topicPattern)
def stream[K, V](offsetReset: TopologyBuilder.AutoOffsetReset,
topicPattern: Pattern)
(implicit keySerde: Serde[K], valSerde: Serde[V]): KStreamS[K, V] =
inner.stream[K, V](offsetReset, keySerde, valSerde, topicPattern)
def table[K, V](topic: String,
storeName: String)
(implicit keySerde: Serde[K], valSerde: Serde[V]): KTableS[K, V] =
inner.table[K, V](keySerde, valSerde, topic, storeName)
def table[K, V](offsetReset: TopologyBuilder.AutoOffsetReset,
topic: String,
storeName: String)
(implicit keySerde: Serde[K], valSerde: Serde[V]): KTableS[K, V] =
inner.table[K, V](offsetReset, keySerde, valSerde, topic, storeName)
def globalTable[K, V](topic: String,
storeName: String)
(implicit keySerde: Serde[K], valSerde: Serde[V]): GlobalKTable[K, V] =
inner.globalTable(keySerde, valSerde, topic, storeName)
def merge[K, V](streams: KStreamS[K, V]*): KStreamS[K, V] = {
val streamsJ = streams.map { streamS => streamS.inner }
inner.merge(streamsJ: _*)
}
}
示例12: RegexConstraint
//设置package包名称以及导入依赖的类
package be.dataminded.wharlord.constraints
import java.util.regex.Pattern
import org.apache.spark.sql.functions._
import org.apache.spark.sql.{Column, DataFrame}
import scala.util.Try
case class RegexConstraint(columnName: String, regex: String) extends Constraint {
val fun = (df: DataFrame) => {
val pattern = Pattern.compile(regex)
val doesNotMatch = udf((column: String) => column != null && !pattern.matcher(column).find())
val maybeDoesNotMatchCount = Try(df.filter(doesNotMatch(new Column(columnName))).count)
RegexConstraintResult(
constraint = this,
data = maybeDoesNotMatchCount.toOption.map(RegexConstraintResultData),
status = tryToStatus[Long](maybeDoesNotMatchCount, _ == 0)
)
}
}
case class RegexConstraintResult(constraint: RegexConstraint,
data: Option[RegexConstraintResultData],
status: ConstraintStatus) extends ConstraintResult[RegexConstraint] {
val message: String = {
val columnName = constraint.columnName
val regex = constraint.regex
val maybeFailedRows = data.map(_.failedRows)
val maybePluralSAndVerb = maybeFailedRows.map(failedRows => if (failedRows == 1) ("", "does") else ("s", "do"))
(status, maybeFailedRows, maybePluralSAndVerb) match {
case (ConstraintSuccess, Some(0), _) =>
s"Column $columnName matches $regex"
case (ConstraintFailure, Some(failedRows), Some((pluralS, verb))) =>
s"Column $columnName contains $failedRows row$pluralS that $verb not match $regex"
case (ConstraintError(throwable), None, None) =>
s"Checking whether column $columnName matches $regex failed: $throwable"
case default => throw IllegalConstraintResultException(this)
}
}
}
case class RegexConstraintResultData(failedRows: Long)
示例13: uuid
//设置package包名称以及导入依赖的类
package events
import eventstore.JsonMapping._
import java.util.UUID
import java.util.regex.Pattern
import play.api.libs.json._
import scala.util.control.Exception.catching
trait Identifier {
def uuid: UUID
}
abstract class IdentifierCompanion[A <: Identifier](val prefix: String) {
def apply(uuid: UUID): A
def generate(): A = apply(UUID.randomUUID)
def fromString(s: String): Option[A] = s match {
case IdentifierRegex(uuid) => catching(classOf[RuntimeException]) opt { apply(UUID.fromString(uuid)) }
case _ => None
}
implicit val IdentifierFormat: Format[A] = valueFormat(apply)(_.uuid)
implicit val IdentifierCompanionObject: IdentifierCompanion[A] = this
private val IdentifierRegex = (Pattern.quote(prefix) + """\(([a-fA-F0-9-]{36})\)""").r
}
示例14: Template
//设置package包名称以及导入依赖的类
package de.frosner.broccoli.models
import java.util.regex.Pattern
import org.apache.commons.codec.digest.DigestUtils
import play.api.libs.json._
import play.api.libs.functional.syntax._
import scala.collection.mutable.ArrayBuffer
import ParameterInfo.parameterInfoWrites
case class Template(id: String, template: String, description: String, parameterInfos: Map[String, ParameterInfo])
extends Serializable {
@transient
lazy val parameters: Set[String] = {
val matcher = Template.TemplatePattern.matcher(template)
var variables = ArrayBuffer[String]()
while (matcher.find()) {
variables += matcher.group(1)
}
val uniqueVariables = variables.toSet
require(
uniqueVariables.contains("id"),
s"There needs to be an 'id' field in the template for Broccoli to work. Parameters defined: ${uniqueVariables}")
uniqueVariables
}
@transient
lazy val version: String = DigestUtils.md5Hex(template.trim() + "_" + parameterInfos.toString)
}
object Template {
val TemplatePattern = Pattern.compile("\\{\\{([A-Za-z][A-Za-z0-9\\-\\_\\_]*)\\}\\}")
implicit val templateApiWrites: Writes[Template] = (
(JsPath \ "id").write[String] and
(JsPath \ "description").write[String] and
(JsPath \ "parameters").write[Set[String]] and
(JsPath \ "parameterInfos").write[Map[String, ParameterInfo]] and
(JsPath \ "version").write[String]
)((template: Template) =>
(template.id, template.description, template.parameters, template.parameterInfos, template.version))
implicit val templatePersistenceReads: Reads[Template] = Json.reads[Template]
implicit val templatePersistenceWrites: Writes[Template] = Json.writes[Template]
}
示例15: DataType
//设置package包名称以及导入依赖的类
package com.epam.streaming
import java.io.File
import java.util.regex.Pattern
import org.apache.spark.SparkContext
import org.apache.spark.rdd.RDD
import scala.collection.mutable
object DataType extends Enumeration {
val ActorData, RatingData = Value
}
class ImdbEventGenerator(actorDataPath: String, ratingDataPath: String) {
val YEAR_IN_FILE_NAME_PATTERN: Pattern = Pattern.compile("^.+_(\\d+)\\.tsv$")
val yearsOfRatings = getYearsFromListOfFiles(ratingDataPath)
val yearsOfActors = getYearsFromListOfFiles(actorDataPath)
val years: List[String] = yearsOfRatings.intersect(yearsOfActors).toList.sorted
def buildImdbEventStream(sc: SparkContext, dataType: DataType.Value): mutable.Queue[RDD[String]] = {
val queue = mutable.Queue[RDD[String]]()
val yearRDDs = years.map(year => sc.parallelize(loadLinesFromFile(year, dataType)))
queue ++= yearRDDs
}
def getYearsFromListOfFiles(dir: String): Set[String] = {
val d = new File(dir)
if (d.exists && d.isDirectory) {
d.listFiles
.filter(_.isFile)
.map(_.getName)
.map(YEAR_IN_FILE_NAME_PATTERN.matcher(_))
.filter(_.matches())
.map(_.group(1))
.toSet
} else {
Set[String]()
}
}
def loadLinesFromFile(year: String, dataType: DataType.Value): Seq[String] = {
val fileName = dataType match {
case DataType.ActorData => s"$actorDataPath/actor_data_$year.tsv"
case DataType.RatingData => s"$ratingDataPath/movie_ratings_$year.tsv"
}
val source = scala.io.Source.fromFile(fileName)
try source.getLines().filter(!_.trim.isEmpty).toList finally source.close()
}
}