learning scala regular expression patterns

package com.aura.scala.day01

import scala.util.matching.Regex


object regularExpressionPatterns {
  def main(args: Array[String]): Unit = {
    // example 1
    // .r 方法可便任意字符串变成一个正则表达式
    val numberPatter: Regex = "[0-9]".r
    numberPatter.findFirstMatchIn("dsafasdfasfasfas") match {
      case Some(_) => println("Password OK")
      case None => println("Password must contain a number")
    }
    // example 2
    val KeyValPattern: Regex = "([0-9A-Za-z-#() ]+):([0-9A-Za-z-#() ]+)".r
    val input: String =
      """
        |background-color: #A03300;
        |background-image: url(img/header100.png);
        |background-position: top center;
        |background-repeat: repeat-x;
        |background-size: 2160px 108px
        |margin: 0;
        |height: 108px;
        |width: 100%;
      """.stripMargin
     for( patternMath <-  KeyValPattern.findAllMatchIn(input))
       println(s"key:${patternMath.group(1)} value:${patternMath.group(2)}")
  }
}

output:

"D:\Program Files\Java\jdk1.8.0_201\bin\java.exe" "-javaagent:D:\Program Files\JetBrains\IntelliJ IDEA 2019.1.3\lib\idea_rt.jar=8599:D:\Program Files\JetBrains\IntelliJ IDEA 2019.1.3\bin" -Dfile.encoding=UTF-8 -classpath "D:\Program Files\Java\jdk1.8.0_201\jre\lib\charsets.jar;D:\Program Files\Java\jdk1.8.0_201\jre\lib\deploy.jar;D:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\access-bridge-64.jar;D:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\cldrdata.jar;D:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\dnsns.jar;D:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\jaccess.jar;D:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\jfxrt.jar;D:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\localedata.jar;D:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\nashorn.jar;D:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\sunec.jar;D:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\sunjce_provider.jar;D:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\sunmscapi.jar;D:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\sunpkcs11.jar;D:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\zipfs.jar;D:\Program Files\Java\jdk1.8.0_201\jre\lib\javaws.jar;D:\Program Files\Java\jdk1.8.0_201\jre\lib\jce.jar;D:\Program Files\Java\jdk1.8.0_201\jre\lib\jfr.jar;D:\Program Files\Java\jdk1.8.0_201\jre\lib\jfxswt.jar;D:\Program Files\Java\jdk1.8.0_201\jre\lib\jsse.jar;D:\Program Files\Java\jdk1.8.0_201\jre\lib\management-agent.jar;D:\Program Files\Java\jdk1.8.0_201\jre\lib\plugin.jar;D:\Program Files\Java\jdk1.8.0_201\jre\lib\resources.jar;D:\Program Files\Java\jdk1.8.0_201\jre\lib\rt.jar;C:\Users\admin\IdeaProjects\ScalaDay\out\production\ScalaDay;C:\Program Files (x86)\scala\lib\scala-actors-2.11.0.jar;C:\Program Files (x86)\scala\lib\scala-actors-migration_2.11-1.1.0.jar;C:\Program Files (x86)\scala\lib\scala-library.jar;C:\Program Files (x86)\scala\lib\scala-parser-combinators_2.11-1.0.4.jar;C:\Program Files (x86)\scala\lib\scala-reflect.jar;C:\Program Files (x86)\scala\lib\scala-swing_2.11-1.0.2.jar;C:\Program Files (x86)\scala\lib\scala-xml_2.11-1.0.4.jar" com.aura.scala.day01.regularExpressionPatterns
Password must contain a number
key:background-color value: #A03300
key:background-image value: url(img
key:background-position value: top center
key:background-repeat value: repeat-x
key:background-size value: 2160px 108px
key:margin value: 0
key:height value: 108px
key:width value: 100

 

posted @ 2019-07-10 14:52  嵌入式实操  阅读(193)  评论(0编辑  收藏  举报