は、私は私が定義された牽引正規表現パターンのいずれかと一致する文字列の一部を抽出したい:パターンマッチングエキス文字列スカラ
val matcher= (s:String) => s match {case pPat(el)=> println(el) // print the P.25.01.25
case rPat(el)=>println(el) // print R0100
case _ => println("no match")}
:私は今のように要素を抽出するために私のメソッドを定義
//should match R0010, R0100,R0300 etc
val rPat="[R]{1}[0-9]{4}".r
// should match P.25.01.21 , P.27.03.25 etc
val pPat="[P]{1}[.]{1}[0-9]{2}[.]{1}[0-9]{2}[.]{1}[0-9]{2}".r
そして、それをテストするなどして:私は、正規表現式が間違っているかはわからないけど
val pSt=" P.25.01.21 - Hello whats going on?"
matcher(pSt)//prints "no match" but should print P.25.01.21
val rSt= "R0010 test test 3,870"
matcher(rSt) //prints also "no match" but should print R0010
//check if regex is wrong
val pHead="P.25.01.21"
pHead.matches(pPat.toString)//returns true
val rHead="R0010"
rHead.matches(rPat.toString)//return true
matchesメソッドの作品要素上に。だから、このアプローチの何が間違っていますか?
ありがとうございました! –