Created
February 18, 2016 00:30
-
-
Save ganeshchand/d54b2a668953e43c0e20 to your computer and use it in GitHub Desktop.
scala-regex.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val lines = Array( | |
"CA94401,CA1,02,CB2,03,K1,Y2,10,20", | |
"CA94402,CA2,03,CB3,043,L1,Z2,10,20") | |
lines.filter(line => line.contains("CA94401")) | |
val kv = lines.map( | |
line => | |
(line.split(",")(0), | |
line.split(",").filter(str => str.matches("^[A-Za-z][0-9]*$")).mkString(","))) // (k,v) | |
//output kv: Array[(String, String)] = Array((CA94401,K1,Y2), (CA94402,L1,Z2)) | |
"CA1".matches("^[A-Za-z]+[0-9].$") | |
"CA1".matches("CA1") | |
val regex = "^[A-Za-z][0-9]*$".r | |
val tokens = List("abc", "ab12", "12", "xy1","X1","y12") | |
tokens.filter(token => regex.pattern.matcher(token).matches()) // res3: List[String] = List(X1, y12) | |
"abc,ab12,12,xy1,X1,y12".split(",").filter(str => str.matches("^[A-Za-z][0-9]*$")) //res4: Array[String] = Array(X1, y12) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment