tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
|
|
@ -0,0 +1,4 @@
|
|||
val Bottles1 = "(\\d+) bottles of beer".r // syntactic sugar
|
||||
val Bottles2 = """(\d+) bottles of beer""".r // using triple-quotes to preserve backslashes
|
||||
val Bottles3 = new scala.util.matching.Regex("(\\d+) bottles of beer") // standard
|
||||
val Bottles4 = new scala.util.matching.Regex("""(\d+) bottles of beer""", "bottles") // with named groups
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
"99 bottles of beer" matches "(\\d+) bottles of beer" // the full string must match
|
||||
"99 bottles of beer" replace ("99", "98") // Single replacement
|
||||
"99 bottles of beer" replaceAll ("b", "B") // Multiple replacement
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
"\\d+".r findFirstIn "99 bottles of beer" // returns first partial match, or None
|
||||
"\\w+".r findAllIn "99 bottles of beer" // returns all partial matches as an iterator
|
||||
"\\s+".r findPrefixOf "99 bottles of beer" // returns a matching prefix, or None
|
||||
Bottles4 findFirstMatchIn "99 bottles of beer" // returns a "Match" object, or None
|
||||
Bottles4 findPrefixMatchOf "99 bottles of beer" // same thing, for prefixes
|
||||
val bottles = (Bottles4 findFirstMatchIn "99 bottles of beer").get.group("bottles") // Getting a group by name
|
||||
val Bottles4(bottles) = "99 bottles of beer" // syntactic sugar (not using group name)
|
||||
13
Task/Regular-expressions/Scala/regular-expressions-4.scala
Normal file
13
Task/Regular-expressions/Scala/regular-expressions-4.scala
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
val Some(bottles) = Bottles4 findPrefixOf "99 bottles of beer" // throws an exception if the matching fails; full string must match
|
||||
for {
|
||||
line <- """|99 bottles of beer on the wall
|
||||
|99 bottles of beer
|
||||
|Take one down, pass it around
|
||||
|98 bottles of beer on the wall""".stripMargin.lines
|
||||
} line match {
|
||||
case Bottles1(bottles) => println("There are still "+bottles+" bottles.") // full string must match, so this will match only once
|
||||
case _ =>
|
||||
}
|
||||
for {
|
||||
matched <- "(\\w+)".r findAllIn "99 bottles of beer" matchData // matchData converts to an Iterator of Match
|
||||
} println("Matched from "+matched.start+" to "+matched.end)
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
Bottles2 replaceFirstIn ("99 bottles of beer", "98 bottles of beer")
|
||||
Bottles3 replaceAllIn ("99 bottles of beer", "98 bottles of beer")
|
||||
Loading…
Add table
Add a link
Reference in a new issue