Regular expressions: Difference between revisions
Content added Content deleted
m (→{{header|Python}}: fix 1 line) |
|||
Line 1,664:
=={{header|Scala}}==
[[Category:Scala Implementations]]
{{libheader|Scala}}
Define
<lang
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
Line 1,681 ⟶ 1,683:
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</lang>
Using pattern matching with regex:
<lang
for {
line <- """|99 bottles of beer on the wall
Line 1,700 ⟶ 1,701:
Replacing with regex:
<lang
Bottles3 replaceAllIn ("99 bottles of beer", "98 bottles of beer")</lang>
|