Skip to content

Instantly share code, notes, and snippets.

@benve
Created June 8, 2011 09:48
Show Gist options
  • Save benve/1014119 to your computer and use it in GitHub Desktop.
Save benve/1014119 to your computer and use it in GitHub Desktop.
Esempi in Scala
//http://daily-scala.blogspot.com/2010/01/regular-expression-3-regex-matching.html
val date = "11/01/2010"
val Date = """(\d\d)/(\d\d)/(\d\d\d\d)""".r
val Date(day, month, year) = date
println(day)
println(month)
println(year)
//Match
val estrai = """([^=]*)=(.*)""".r
def getStrategy(locator : String) : By = locator match {
case l if l.startsWith("//") => By.xpath(l)
case estrai("id", v) => By.id(v)
case estrai("name", v) => By.name(v)
case estrai("class", v) => By.className(v)
case estrai("xpath", v) => By.xpath(v)
case _ => throw new Exception("Errore "+ locator)
}
val xpath = "//ul[@id='mainForm:desktop_menu']/li/span/a[normalize-space(span)='MDD2']"
val values = Map(
"id=pippo" -> By.id("pippo"),
"name=pippo" -> By.name("pippo"),
"class=pippo" -> By.className("pippo"),
"xpath="+xpath -> By.xpath(xpath),
xpath -> By.xpath(xpath)
)
values.foreach { p =>
assertEquals(p._2, DefaultCommandHandler.getStrategy(p._1))
}
import scala.xml._
def rem(conf: Node): Node = conf match {
case <b>{ ch @ _* }</b> => <b>{ ch.map(rem2) }</b>
case Elem(prefix, label, attr, scope, ch@_*) => Elem(prefix, label, attr, scope, ch.map(rem): _*)
case other@_ => other
}
def rem2(conf: NodeSeq) = conf match {
case e : Elem if (e.label.startsWith("pp.") && (e \ "name").text == "Pippo") => NodeSeq.Empty
case other@_ => other
}
//Rimuove il tag pp.h riconoscendo name==Pippo
val xml = <a>
<b>
<pp.h>
<name>Pippo</name>
<prop>1</prop>
<prop>8</prop>
</pp.h>
<pp.g>
<name>Pluto</name>
<prop>1</prop>
<prop>2</prop>
</pp.g>
</b>
<c/>
</a>
println(rem(xml))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment