I'm looking to parse a string that can be in one of the following formats:
"[a]"
"[a-b]"
"[a-b/9]"
"[a-b, b-c]"
"[a-b/9, b-c]"
In words, the part after - is optional, and if present, may in turn have an optional weight separated by /. The separator character - may change.
Here's my code (executable code here):
import scala.util.matching.Regex
case class Edge[A, B](u: A, v: A, data: Option[B])
def parseEdge(
s: String,
sep: Char
): (List[Edge[String, String]], List[String]) =
if !s.startsWith("[") || !s.endsWith("]")
then throw IllegalArgumentException("string must be enclosed by '[' and ']'")
else
val edgePattern: Regex = raw"""^(.+?)(?:$sep(.+?)(?:\/(.+?))??)??$$""".r
s
.substring(1, s.length() - 1)
.split(",")
.map(_.trim())
.foldRight((List.empty[Edge[String, String]], List.empty[String])) {
case (x, (es, vs)) =>
x match
case edgePattern(u) => (es, u :: vs)
case edgePattern(u, v) => (Edge(u, v, None) :: es, vs)
case edgePattern(u, v, d) => (Edge(u, v, Some(d)) :: es, vs)
}
But:
println(parseEdge("[b-c]", '-')) // (List(Edge(b,c,Some(null))),List())
println(parseEdge("[b-c/9]", '-')) // (List(Edge(b,c,Some(9))),List())
Why's the first string parsed with a null instead of a None?