2014-10-22 2 views
0

이 문제를 해결할 수있는 방법이 있습니까?Regex Tags tr 태그 안에있는 모든 td 태그를 무시하십시오.

tr 태그의 모든 td 태그를 무시하는 정규식이 필요합니다. 끝 태그에 "/"이 누락되어 있으므로 찾고있는 tr 태그가 잘못되었습니다. 지금까지 내가 가지고 :

<tr[^>]*><td(?:(?!</td>).)*</td><tr[^>]*> 

<tr[^>]*> This needs to be the beginning of the expression **** 

<td(?:(?!</td>).)*</td> This will find everything between <td> and </td> 

<tr[^>]*> This needs to be the end of the expression **** 

이 정규식은 물론 작동하지 않습니다.

샘플 1 :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <title>     
    </title> 
</head>        
<body> 
    <table asdf> 
     <tr asdf> 
     <td asdf> 
      <table asdf> 
       <tr asdf: asdf> 
        <td> 
         blah blah blah 
        </td> 
       </tr> 
      </table> 
      </td> 
      <td> 
       Keep going 
      </td> 
     <tr> If highlighted to here from first tr tag than correct regex was used 
    </table> 
</body> 
</html> 

샘플 2 :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <title>     
    </title> 
</head>        
<body> 
    <table asdf> 
     <tr asdf> 
     <td asdf> 
      <table asdf> 
       <tr asdf: asdf> 
        <td> 
         blah blah blah 
        </td> 
       </tr> 
      </table> 
      </td> 
      <td> 
       <table asdf> 
       <tr asdf: asdf> 
        <td> 
         blah blah blah 
        </td> 
       </tr> 
      </table> 
      </td> 
     <tr> If highlighted to here from first tr tag than correct regex was used 
    </table> 
</body> 
</html> 

샘플 3 :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <title>     
    </title> 
</head>        
<body> 
    <table asdf> 
     <tr asdf> 
     <td asdf> 
      <table asdf> 
       <tr asdf: asdf> 
        <td> 
         blah blah blah 
        </td> 
       </tr> 
      </table> 
      </td> 
      <td> 
       <table> 
       <tr> 
        <td> 
         blah blah blah 
        </td> 
       </tr> 
      </table> 
      </td> 
     <tr> If highlighted to here from first tr tag than correct regex was used 
    </table> 
</body> 
</html> 

샘플 4 여기에 정규 표현식을 실행하는 텍스트의 예입니다 :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <title>     
    </title> 
</head>        
<body> 

<table> 
    <tr> 
     <td>&nbsp;</td> 
    </tr> 
</table> 
<br/> 
<br/> 
<br/> 
<table class="afdadsf"> 
    <td></td> 
</table> 
<br/> 
<br/> 
<table class="fdafdas"> 
    <tr><td></td> 
      </tr> 
    </table> 
</body> 
</html> 

원하는 출력은 정규 표현식을 실행할 때 위의 두 예제 텍스트를 모두 사용하여 마지막 tr 태그가 강조 표시 될 때까지 첫 번째 tr 태그가 강조 표시 될 때의 결과입니다. td 태그가 가능한 모든 값을 포함 할 수있는 다른 샘플 텍스트를 가정하십시오.

+0

당신은''모든''태그를 무시하고 싶지만,''문제의 부족이 아니다? 원하는 출력을 포함시켜야합니다. –

+0

의 부족하다고 생각하지 않습니다. 나는 태그가 으로 끝나는 것을 찾고 있기 때문에 정규 표현식의 끝과 처음에 ] *>을 가져야합니다. 원하는 출력은 샘플 텍스트의 코드 섹션에 설명되어 있습니다. 내 주석을 편집하고 코드 섹션 외부로 옮깁니다. – developer234

답변

0

게시하고 요청 된 내용에 따라, 귀하의 정규식 엔진은 재귀이 패턴 (?R) 사용을 지원하는 경우 :

<tr[^>]*>.*(<(\S+)[^>]*>([^<]|(?1))*?<\/\2>).*?<tr[^>]*> 

아래의 의견에 따라 일부 광범위한 테스트
Demo


을해야 할 수도 있습니다를 <tr> 태그가 항상 가장 외곽에 있으며,이 패턴을 사용하려면 s 옵션을 사용하십시오.

(<tr[^>]*>.*<tr>) 

Demo

+0

건너 뛰기 란 무엇을 의미합니까? 질문을 업데이트하고 예상되는 결과를 게시 하시겠습니까? 이 업데이트 된 예제 일치 항목 # 1은 http://regex101.com/r/yW4aZ3/95를 강조 표시하라는 메시지입니다. –

+0

내 질문을 편집했으며 여기에 대한 내 우려 사항이 있습니다. 1.이 정규 표현식을에서 사용했습니다. 모든 파일 및 표현식이 없으면 파일의 모든 내용을 강조 표시합니다. 나는 수천 개의 파일을 가지고있다. 2. 정규식은 해당 샘플 텍스트에만 적용됩니다. – developer234

+0

질문, 마지막으로 "틀린"후 ""이 (가) " .."개의 태그가 더있을 가능성이 있습니까? 즉 잘못된 태그가 항상 가장 바깥 쪽 태그입니까? –

관련 문제