Scala的正则表达式regex操作
2014-10-16 12:20
351 查看
9.misc
Scala-json
http://www.lag.net/configgy/
简单配置及logging:
----------------------------
log{
filename="/var/log/pingd.log"
roll="daily"
level="debug"
}
hostname="pingd.example.com"
port=3000
----------------------------
valconf=net.lag.configgy.Configgy.configure("/etc/pingd.conf").config
valhostname=conf.getString("hostname","localhost")
valport=conf.getInt("port",3000)
例子1:完全匹配
//将字符串转为scala.util.matching.Regex
valpattern="^[a-z0-9._%\\-+]+@(?:[a-z0-9\\-]+\\.)+[a-z]{2,4}$"
valemailRegex=pattern.r//或者new
scala.util.matching.Regex(pattern)
//emailRegex.pattern=>java.util.regex.Pattern使用java的Pattern
emailRegex.pattern.matcher("tt@16.cn").matches//
true
例子2:查找匹配部分
valp="[0-9]+".r
p.findAllIn("2ad12ab
ab2123").toList//List(2,12,21,23)
p.findFirstMatchIn("abc123xyz").get//
scala.util.matching.Regex.Match=123
更多例子如下:
定义:
search和replace(java.lang.String的方法):
search(regex的方法):
match(regex的方法):
输出:
Matchedfrom0to2
Matchedfrom3to8
replace(regex的方法):
r2replaceFirstIn("99lines-a","aaa")//"aaa-a"
r2replaceAllIn("99lines-a,98lines-b","bbb")//"bbb-a,bbb-b"
其他:使用正则表达式定义变量
valregex="(\\d+)/(\\d+)/(\\d+)".r
valregex(year,month,day)="2010/1/13"
//year:String=2010
//month:String=1
//day:String=13
importswing._,swing.Swing._
objectSwing1extendsSimpleGUIApplication{//
scala2.7
deftop=newMainFrame{//必须实现top方法
title="窗口1"
preferredSize=(400,300)
vallabel=newLabel("hello
worldcn中文")
contents=newBorderPanel{
layout(label)
=BorderPanel.Position.Center
}
}
}
9.misc
9.1.json
Scala-json
9.2.Configgy
简单配置及logging:
----------------------------
log{
filename="/var/log/pingd.log"
roll="daily"
level="debug"
}
hostname="pingd.example.com"
port=3000
----------------------------
valconf=net.lag.configgy.Configgy.configure("/etc/pingd.conf").config
valhostname=conf.getString("hostname","localhost")
valport=conf.getInt("port",3000)
9.3.正则表达式regex
例子1:完全匹配//将字符串转为scala.util.matching.Regex
valpattern="^[a-z0-9._%\\-+]+@(?:[a-z0-9\\-]+\\.)+[a-z]{2,4}$"
valemailRegex=pattern.r//或者new
scala.util.matching.Regex(pattern)
//emailRegex.pattern=>java.util.regex.Pattern使用java的Pattern
emailRegex.pattern.matcher("tt@16.cn").matches//
true
例子2:查找匹配部分
valp="[0-9]+".r
p.findAllIn("2ad12ab
ab2123").toList//List(2,12,21,23)
p.findFirstMatchIn("abc123xyz").get//
scala.util.matching.Regex.Match=123
更多例子如下:
定义:
valr1="(\\d+)lines".r//syntacticsugar valr2="""(\d+)lines""".r//usingtriple-quotestopreservebackslashes
或者
importscala.util.matching.Regexvalr3= newRegex("(\\d+)lines")//standard valr4= newRegex("""(\d+)lines""","lines")//withnamedgroups
search和replace(java.lang.String的方法):
"99lines"matches"(\\d+)lines"//true
"99Lines"matches"(\\d+)lines"//false
"99lines"replace("99","98")//"98lines"
"99lineslines"replaceAll("l","L")//99LinesLines
search(regex的方法):
"\\d+".rfindFirstIn"99lines"//Some(99)
"\\w+".rfindAllIn"99lines"//iterator(长度为2)
"\\s+".rfindPrefixOf"99lines"//None
"\\s+".rfindPrefixOf"99lines"//Some()
valr4= newRegex("""(\d+)lines""","g1")//withnamedgroups
r4findFirstMatchIn"99lines-a"//Some(99lines)
r4findPrefixMatchOf"99lines-a"//Some(99lines)
valb=(r4findFirstMatchIn"99lines").get.group("g1")//"99"
match(regex的方法):
valr1="(\\d+)lines".r
valr4= newRegex("""(\d+)lines""","g1")
valSome(b)=r4findPrefixOf"99lines"//"99lines" for{line<-"""|99lines-a|99lines|pass|98lines-c""".stripMargin.lines}line match{ caser1(n)=>println("Has"+n+"Lines.")//"Has99Lines."
case_=>}
for(matched<-"(\\w+)".rfindAllIn"99lines"matchData)
println("Matchedfrom"+matched.start+"to"+matched.end)
输出:
Matchedfrom0to2
Matchedfrom3to8
replace(regex的方法):
valr2="""(\d+)lines""".r//usingtriple-quotestopreservebackslashes
r2replaceFirstIn("99lines-a","aaa")//"aaa-a"
r2replaceAllIn("99lines-a,98lines-b","bbb")//"bbb-a,bbb-b"
其他:使用正则表达式定义变量
valregex="(\\d+)/(\\d+)/(\\d+)".r
valregex(year,month,day)="2010/1/13"
//year:String=2010
//month:String=1
//day:String=13
9.4.GUI
9.4.1.java方式
importjavax.swing.JFrame
varjf= newJFrame("Hello!")
jf.setSize(800,600)
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
jf.setVisible(true)
9.4.2.scala方式
importswing._,swing.Swing._objectSwing1extendsSimpleGUIApplication{//
scala2.7
deftop=newMainFrame{//必须实现top方法
title="窗口1"
preferredSize=(400,300)
vallabel=newLabel("hello
worldcn中文")
contents=newBorderPanel{
layout(label)
=BorderPanel.Position.Center
}
}
}
相关文章推荐
- [置顶] 【Scala 正则表达式】Regex 类来支持正则表达式
- Java中的正则表达式(Java Regex)
- 常见正则表达式运算 Regex 替换字符
- Delphi 正则表达式之TPerlRegEx 类的属性与方法(3): Start、Stop
- [Regex]ASP.NET 中的正则表达式-微软速成课程
- [Regex]ASP.NET 中的正则表达式-微软速成课程
- Delphi 正则表达式之TPerlRegEx 类的属性与方法(7): Split 函数
- 使用正则表达式Regex,对验证数据的输入
- Delphi 正则表达式之TPerlRegEx 类的属性与方法(1): 查找
- [Regex]ASP.NET 中的正则表达式-微软速成课程
- [Regex]ASP.NET 中的正则表达式-微软速成课程
- Regex 正则表达式
- Python正则表达式操作指南
- 正则表达式测试器0.3(Boost Regex Tester 0.3) (老版保留)
- Regular Expressions的一个工具:T-Regex 正则表达式验证工具
- Delphi 正则表达式之TPerlRegEx 类的属性与方法(2): 关于子表达式
- [Regex]笑容的正则表达式大全
- Regex 深入正则表达式
- Delphi 正则表达式之TPerlRegEx 类的属性与方法(5): Compile、Study
- [Regex]笑容的正则表达式大全