正则表达式

正则表达式

规则适用于所有编程语言

py 复制代码
container_pattern = (
        r'<div class="container">\s*'#路过,不要
        r'(<div class="title-wrapper">[\s\S]*?</div>\s*'   
        r'<div class="stats">[\s\S]*?</div>)\s*'#捕获
        r'</div>\s*'#路过
        r'<div class="chart-container">\s*'#路过
        r'(<div id="chart"[^>]*></div>)\s*'                #捕获
        r'</div>\s*</div>'#路过
    )
container_match = re.search(container_pattern, source_html, re.DOTALL)

正则在这里只做一件事:在源文件里找一段文字。

  • 没有括号 (...) 的部分 → 只是「路过」,用来定位
  • 有括号 (...) 的部分 → 才会被「抓出来」,放进 group(1)、group(2)
相关推荐
小小龙学IT17 小时前
C++ 正则表达式完全指南:从 std::regex 实战到 RE2 引擎原理(NFA/DFA/回溯陷阱)
c++·正则表达式
傻啦嘿哟19 小时前
猫眼电影TOP100爬虫:Requests+正则表达式实战
爬虫·正则表达式
circuitsosk20 小时前
AI输出的“质检员”:构建智能体质量评估、异常检测与人工兜底的三层防线
人工智能·python·microsoft·正则表达式·langchain
Livia要学习1 天前
Python正则表达式
正则表达式
wangchen_03 天前
C++正则表达式
开发语言·c++·正则表达式
Patrick在香港4 天前
Python正则表达式实战:解析和验证香港身份证、车牌、电话格式
开发语言·python·正则表达式
吴声子夜歌6 天前
正则指引——Ruby
开发语言·正则表达式·ruby
吴声子夜歌6 天前
正则指引——Golang
开发语言·golang·正则表达式
吴声子夜歌7 天前
正则指引——PHP
开发语言·正则表达式·php