>

여기 내 코드가 있습니다 :

object test extends App {
  private val PLAYER_REGEX = """[\s\S]*(?:<td class="align-middle plus-size"> <s class="text-muted">|<td class="align-middle plus-size">)(.*)(?:</s> </td></tr>|</td></tr>)""".r
  val str ="""<td class="align-middle plus-size"> <s class="text-muted">first</s> </td></tr>"""
  val str2 ="""<td class="align-middle plus-size">second</td></tr>"""
  private def find(str:String) = {
    PLAYER_REGEX.findFirstMatchIn(str) match {
      case Some(data) => data.group(1).trim
      case None => "Not found"
    }
  }
  println(find(str))
  println(find(str2))
}

그리고 출력은

first</s>
second

내 질문은-왜 중복 되는가

</s>

첫 번째 경우? 나는 그것을 생각했다

(?:</s> </td></tr>|</td></tr>) 

첫 번째 발생을 선택해야합니다

</s> </td></tr>

하지만 선택된 것처럼 보입니다

</td></tr>???

물론, 나는 그것을 다듬을 수 있지만, 추악하게 보입니다. 다른 정규 표현식을 제공 할 수 있다면 기쁠 것입니다 :)

  • 답변 # 1

    이 욕심 많은 수량 자에 의해 사전에 발생되었습니다 :

    …(.*)…
    
    

    대신 게으른 버전을 사용하십시오 :

    …(.*?)…
    
    

  • 이전 C # 프로젝트에서 UML 다이어그램 작성
  • 다음 ReactJS - reactjs - 보다 반응적인 데이터 방식을 보여주기 위해 테이블 ​​행을 두 행으로 나누는 방법