Valerin
Valerin

Reputation: 475

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 :)???

Upvotes: 1

Views: 142

Answers (1)

mohit
mohit

Reputation: 4999

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

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

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

Upvotes: 3

Related Questions