【问题标题】:Regular Expression extracting 2 pieces of information正则表达式提取2条信息
【发布时间】:2020-12-27 19:14:08
【问题描述】:

目前我有很多 html 页面,我需要提取 2 条信息。我当前使用的表达式允许我提取一个信息,如果我需要同时提取 2 条数据怎么办。

(?s)\A.*(var vpart=".*?";var pn).*\Z replace $1 

这是我正在使用的表达式,我需要在

标签中提取另一个数据,有人可以帮我修改上面的表达式吗?

【问题讨论】:

  • 使用正则表达式从 HTML 中提取信息是个坏主意。
  • 使用组 (A)(B)... 等等。括号内的任何表达式都称为组。
  • (?s)\A.(.*?)(var vpart=".*?";var pn)*\Z 我试过这个,但似乎没有工作
  • 我知道这是个坏主意,但我找不到任何简单的数据挖掘扩展,简单的无法提取我想要的信息

标签: html regex


【解决方案1】:

是的,使用更多组:

(?s)\A.*(var vpart=".*?";var pn).*(var endpart=".*?";var mn).*\Z

proof

替换为$1\n$2

如果有更多群组,请添加更多\n$X

说明

--------------------------------------------------------------------------------
  (?s)                     set flags for this block (with . matching
                           \n) (case-sensitive) (with ^ and $
                           matching normally) (matching whitespace
                           and # normally)
--------------------------------------------------------------------------------
  \A                       the beginning of the string
--------------------------------------------------------------------------------
  .*                       any character (0 or more times (matching
                           the most amount possible))
--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    var vpart="              'var vpart="'
--------------------------------------------------------------------------------
    .*?                      any character (0 or more times (matching
                             the least amount possible))
--------------------------------------------------------------------------------
    ";var pn                 '";var pn'
--------------------------------------------------------------------------------
  )                        end of \1
--------------------------------------------------------------------------------
  .*                       any character (0 or more times (matching
                           the most amount possible))
--------------------------------------------------------------------------------
  (                        group and capture to \2:
--------------------------------------------------------------------------------
    var endpart="            'var endpart="'
--------------------------------------------------------------------------------
    .*?                      any character (0 or more times (matching
                             the least amount possible))
--------------------------------------------------------------------------------
    ";var mn                 '";var mn'
--------------------------------------------------------------------------------
  )                        end of \2
--------------------------------------------------------------------------------
  .*                       any character (0 or more times (matching
                           the most amount possible))
--------------------------------------------------------------------------------
  \Z                       before an optional \n, and the end of the
                           string

【讨论】:

  • @MurphyKwok 如果答案有助于解决问题,请点击答案左侧的✓,随时to accept
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多