Scala学習ノート9(misc)

7964 ワード

9.   misc
9.1.     json
Scala-json
9.2.     Configgy
http://www.lag.net/configgy/
簡単な構成とlogging:
----------------------------
log {
  filename = "/var/log/pingd.log"
  roll = "daily"
  level = "debug"
}
 
hostname = "pingd.example.com"
port = 3000
----------------------------
 
val conf = net.lag.configgy.Configgy.configure("/etc/pingd.conf").config
val hostname = conf.getString("hostname", "localhost")
val port = conf.getInt("port", 3000)
 
9.3.正規表現regex
例1:完全一致
//文字列をscalaに変換するutil.matching.Regex
val pattern = "^[a-z0-9._%\\-+]+@(?:[a-z0-9\\-]+\\.)+[a-z]{2,4}$"
val emailRegex = pattern.r//またはnew scala.util.matching.Regex(pattern)
//emailRegex.pattern=>java.util.regex.Pattern javaのPatternを使う
emailRegex.pattern.matcher("[email protected]").matches //true
 
例2:一致部分の検索
val p = "[0-9]+".r  
p.findAllIn("2 ad 12ab ab21 23").toList //List(2, 12, 21, 23)
p.findFirstMatchIn("abc123xyz").get //scala.util.matching.Regex.Match = 123
 
次のような例があります.
定義:
val r1 = "(\\d+) lines".r  // syntactic sugar val r2 = """(\d+) lines""".r  // using triple-quotes to preserve backslashes
  
import scala.util.matching.Regex val r3 = new Regex("(\\d+) lines")  // standard val r4 = new Regex("""(\d+) lines""", "lines") // with named groups

 
searchとreplace(java.lang.Stringの方法):
"99 lines" matches "(\\d+) lines" // true
"99 Lines" matches "(\\d+) lines" // false
"99 lines" replace ("99", "98") // "98 lines"
"99 lines lines" replaceAll ("l", "L") // 99 Lines Lines

 
search(regexの方法):
"\\d+".r findFirstIn "99 lines" // Some(99)
"\\w+".r findAllIn "99 lines" // iterator(   2)
"\\s+".r findPrefixOf "99 lines" // None
"\\s+".r findPrefixOf "  99 lines" // Some(  )
val r4 = new Regex("""(\d+) lines""", "g1") // with named groups
r4 findFirstMatchIn "99 lines-a" // Some(99 lines)
r4 findPrefixMatchOf "99 lines-a" // Some(99 lines)
val b = (r4 findFirstMatchIn "99 lines").get.group("g1") // "99"

 
match(regexの方法):
val r1 = "(\\d+) lines".r
val r4 = new Regex("""(\d+) lines""", "g1")
val Some(b) = r4 findPrefixOf "99 lines" // "99 lines" for {   line match {   case r1(n) => println("Has " + n + " Lines.") // "Has 99 Lines."
  case _ => }
 
for (matched findAllIn "99 lines" matchData)
  println("Matched from " + matched.start + " to " + matched.end)

出力:
Matched from 0 to 2
Matched from 3 to 8
 
replace(regexの方法):
val r2 = """(\d+) lines""".r  // using triple-quotes to preserve backslashes

r2 replaceFirstIn ("99 lines-a", "aaa")//"aaa-a"
r2 replaceAllIn ("99 lines-a, 98 lines-b", "bbb")//"bbb-a, bbb-b"
 
その他:正規表現を使用した変数の定義
val regex = "(\\d+)/(\\d+)/(\\d+)".r
val regex(year, month, day) = "2010/1/13"
//year: String = 2010
//month: String = 1
//day: String = 13
 
9.4.     GUI
9.4.1.JAva方式
import javax.swing.JFrame 
 
  
var jf = new JFrame("Hello!") 
jf.setSize(800, 600) 
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) 
jf.setVisible(true)

 
9.4.2.scala方式
import swing._, swing.Swing._
 
object Swing1 extends SimpleGUIApplication { //scala2.7
 
def top=new MainFrame{//topメソッドを実装する必要がある
title="ウィンドウ1"
    preferredSize = (400, 300)
   
val label=new Label(「hello world cn中国語」)
    contents = new BorderPanel {
      layout(label) = BorderPanel.Position.Center
    }
  }
}