There are some strings:
111/aaa
111/aaa|222/bbb
They are in the form of expression:
(.*)/(.*)(|(.*)/(.*))?
I tried to use it to match a string and extract the values:
var rrr = """(.*)/(.*)(|(.*)/(.*))?""".r
"123/aaa|444/bbb" match {
case rrr(pid,pname, cid,cname) => println(s"$pid, $pname, $cid, $cname")
case _ => println("not matched ?!")
}
But it prints:
not matched ?!
And I want to get:
123, aaa, 444, bbb
How to fix it?
UPDATE
Thanks for @BartKiers and @Barmar's anser, that I found my regex has several mistakes, and finally found this solution:
var rrr = """(.*?)/(.*?)([|](.*?)/(.*?))?""".r
"123/aaa|444/bbb" match {
case rrr(pid,pname, _, cid,cname) => println(s"$pid, $pname, $cid, $cname")
case _ => println("not matched ?!")
}
It works, but you can see there is a _ which is actually not useful. Is there any way to redefine the regex that I can just write rrr(pid,pname,cid,cname) to match it?
|is a special character in regexp, you need to escape it.?: