如何在python中包装容器中出现的字符串

2024-06-01 21:58:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个应用程序,呈现HTML和我希望添加一个查找功能,将包装在跨标签,使我可以样式他们的任何匹配。你知道吗

html = """

<html>
    <h1>hello world</h1>
    <p>This is text to display</p>
</html>

"""

match = "h"

match变量中的字符串应为任意长度。你知道吗

注意:我不想匹配标签中出现的任何“h”字符。你知道吗

我愿意用靓汤或其他图书馆。你知道吗

我的HTML:

    <html>
 <h1>
  Kraken-MD
 </h1>
 <br/>
 <hr/>
 <br/>
 View markdown on your computer with ease, launch Kraken MD from command line or open a file!
 <br/>
 <blockquote>
  <span style="color: grey; background-color: grey;">
   |
  </span>
  <b>
   Note:
  </b>
  Only basic markdown capability is present, if you wish to render more advanced markdown, download another renderer, this program is a work in progress and is
  <i>
   not
  </i>
  yet fully capable. Updates are being worked on.
 </blockquote>
 <br/>
 <br/>
 <i>
  Kraken MD
 </i>
 is a markdown renderer for the Windows operating system. Its purpose is to display formatted markdown.
 <br/>
 <br/>
 To begin, open Kraken MD and go to
 <span style="color: maroon;">
  <code>
   File &gt; Open
  </code>
 </span>
 or press CTRL+O and select a
 <span style="color: maroon;">
  <code>
   .md
  </code>
 </span>
 file. Your file will be displayed in the window.
 <br/>
 <br/>
 You can also open a file immediately via the command line. Simply enter
 <span style="color: maroon;">
  <code>
   python index.py
   <filepath>
   </filepath>
  </code>
 </span>
 where
 <span style="color: maroon;">
  <code>
   <filepath>
   </filepath>
  </code>
 </span>
 is an absolute or relative path to the file.
 <br/>
 <br/>
 To run an unbuilt version, you must have installed:
 <br/>
 <ul>
  <li>
   wxPython
   <span style="color: maroon;">
    <code>
     ($ pip install -U wxPython)
    </code>
   </span>
  </li>
 </ul>
 <br/>
 <ul>
  <li>
   beautifulSoup
   <span style="color: maroon;">
    <code>
     ($ pip install beautifulsoup4)
    </code>
   </span>
  </li>
 </ul>
 <br/>
 <ul>
  <li>
   <a href="https://www.python.org/ftp/python/3.6.5/python-3.6.5.exe" style="color:maroon;" target="_blank">
    python 3.6+
   </a>
  </li>
 </ul>
 <br/>
</html>

Tags: tobrisstylehtmlcodeliul
1条回答
网友
1楼 · 发布于 2024-06-01 21:58:47

如果要在HTML中查找并设置包含match字符的某些单词的样式,则可以创建修饰符并使用re

import re

def apply_span(character, span_class='styling_default'):
  def outer(f):
    def wrapper(*args, **kwargs):
      html = f(*args, **kwargs)
      groups = map(str.split, re.findall(r'(?<=\b\w{1}\>)[\w\W]+(?=\</\w{1}\b)|(?<=\b\w{2}\>)[\w\W]+(?=\</\w{2}\b)', html))
      return re.sub(r'(?<=\b\w{1}\>)[\w\W]+(?=\</\w{1}\b)|(?<=\b\w{2}\>)[\w\W]+(?=\</\w{2}\b)', '{}', html).format(*[' '.join('<span class="{}">{}</span>'.format(span_class, i) if character in i else i for i in b) for b in groups])
    return wrapper
  return outer

@apply_span('h')
def return_html(*args, **kwargs):
   html = """
    <html>
      <h1>hello world</h1>
       <p>This is text to display</p>
   </html>
   """
   return html

print(return_html())

输出:

<html>
   <h1><span class="styling_default">hello</span> world</h1>
   <p><span class="styling_default">This</span> is text to display</p>
</html>

span_class的目标是,在HTML中,您可以稍后应用css样式类:

.span_class{
  color:red;
  font-size:20px;

}

相关问题 更多 >