Notepad++를 사용하여 여러 HTML 파일의 태그 내부에 있는 모든 코드 삭제

Notepad++를 사용하여 여러 HTML 파일의 태그 내부에 있는 모든 코드 삭제

포함된 모든 코드를 삭제해야 합니다.

<li class="share"> ... </li>

태그 자체 도 포함됩니다 <li>.

<li>태그 안에는 다른 여러 태그가 있으므로 li class="share"이에 접근하는 방법을 잘 모르겠습니다. 저는 Notepad++를 사용하고 있습니다.

답변1

음, 아래는 다음과 같은 경우에 잘 작동하는 것으로 보이는 신속하게 조합된 코드입니다.단순한데이터 예. 그것으로 당신이 원하는 것을 하십시오.

예, 이것은 메모장에서 간단한 찾기 및 바꾸기로 수행할 수 있는 작업이 아닙니다. 심지어 N++의 F&R에서 사용 가능한 Regex도 실제로 그렇게 할 수 있는 기능을 제공하지 않습니다... 아니면 Regex에서 가능합니다. - 제 수준을 훨씬 뛰어넘습니다. . ;)

import sys
import re

def get_tag():
    buffer = ""
    while True:
        c = sys.stdin.read(1)
        if not c:
            sys.stderr.write("Unexpected EOF\n")
            break
        buffer += c
        if c == '"' or c == "'":
            buffer += get_string(c)
        if c == '>':
            break
    return buffer

def get_string(quote = '"'):
    buffer = ""
    while True:
        c = sys.stdin.read(1)
        if not c:
            sys.stderr.write("Unexpected EOF\n")
            break
        buffer += c
        if c == quote and buffer[-2] != '\\':
            break
    return buffer


buffer = ""
skip_depth = 0

ul_begin = re.compile(r"<\s*li(?:>|\s+.*>)", re.IGNORECASE | re.DOTALL)
ul_begin_share = re.compile(r"<\s*li\s+.*class\s*=\s*([\"'])(?:[^\1]*?\s+)?share(?:\s+[^\1]*?)?(\1).*?>", re.IGNORECASE | re.DOTALL)
ul_end = re.compile(r"</\s*li\s*>", re.IGNORECASE)


while True:
    if skip_depth < 0:
        skip_depth = 0
    c = sys.stdin.read(1)
    if not c:
        #sys.stderr.write("EOF\n")
        break

    if c == '<':
        buffer = c + get_tag()

        if skip_depth > 0 and ul_begin.match(buffer):
            skip_depth += 1
        elif ul_begin_share.match(buffer):
            skip_depth += 1
        elif ul_end.match(buffer):
            skip_depth -= 1
            if skip_depth == 0:
                continue
        c = buffer

    if skip_depth > 0:
        pass
    else:
        sys.stdout.write(c)

data.html의 테스트 데이터:

<ul>
    <li>do not touch that</li>
    <li id="whatever1">or that</li>
    <li class="share">delete this</li>
    <li class="foo-bar share">delete this</li>
    <li class="foobar share foo-bar_">delete this</li>
    <li class='share'>delete this</li>
    <li class='"wtf" share'>delete this</li>
    <li class=" share ">delete this</li>
    <li class="  share  ">delete this</li>
    <li class="foo share">delete this</li>
    <li class="share bar">delete this</li>
    <li class="foo share bar">delete this</li>
    <li class="long foo share short bar">delete this</li>
    <li class=" share ">delete this</li>
    <li class=" foo share bar ">delete this</li>
    <!-- but leave <li class="share">this comment</li> alone -->
    <li>This will stay</li>
    <li class="share">
        <li>delete this</li>
        <li>delete this</li>
    </li>
    <li style="not !important" class="share">delete this</li>
    <li>leave this, but
        <li class="share">
            <li>delete this</li>
            <li>delete this</li>
            <li>delete this</li>
            <li>delete this</li>
        </li>
    </li>
    <li class=" foo share bar ">delete this</li>
    <li class="shared">Can't touch this, naaaa-nanana...</li>
</ul>
<em>blablabla</em>

실행 예시:

$ python test.py < data.html > data.corrected.html
$ cat data.corrected.html
<ul>
    <li>do not touch that</li>
    <li id="whatever1">or that</li>













    <!-- but leave <li class="share">this comment</li> alone -->
    <li>This will stay</li>


    <li>leave this, but

    </li>

    <li class="shared">Can't touch this, naaaa-nanana...</li>
</ul>
<em>blablabla</em>

관련 정보