简体   繁体   中英

Scala - Parsing a string with optional match

I have this pattern:

val smilepattern = "([:]) ([-]) ([) | | | (])".r
val smilepattern(colon, dash, arc) = ": - |" 
println(colon + dash + arc)

my intention is to check the building of three smiles , but HOW I can say that the dash ([-]) is optional? Because, a smile can be :-) and :) ???

You can make things optional by using ? in regular expressions.

scala> ":  )".matches("([:]) ([-]?) ([) | | | (])")
res1: Boolean = true

scala> ": - )".matches("([:]) ([-]?) ([) | | | (])")
res2: Boolean = true

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM