나는 내가 정의 된 견인 정규식 패턴 중 하나와 일치 문자열의 일부를 추출 할 :패턴 일치 추출물 문자열 스칼라
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
일치하는 방법 작품 요소에. 그렇다면이 접근 방식의 문제점은 무엇입니까?
감사합니다. –