转发
我建议使用 HTML 解析模块,因为 HTML 可能会导致一些疯狂的边缘情况,这会真正扭曲您的数据。但是,如果您可以控制您的源文本并且仍然希望/需要使用正则表达式,我会提供这种可能的解决方案。
说明
鉴于以下文字
Example of an item in item list:
<p>
<b>1628/ SomeBoldedTitle
</b>
Some Description.
Some price 20,00kuna.
<strong>Contact somenumber
098/1234-567 some mail
</strong>
</p>
这个正则表达式
<p>(?:(?!<p>).)*<b>([0-9]+)/\s*((?:(?!</b>).)*?)\s*</b>\s*((?:(?!<strong>|<b>).)*?)\s*<(?:strong|b)>\s*((?:(?!</).)*?)\s*</
会将您的文本解析为以下捕获组:
- 第 0 组将是字符串的大部分
- 第 1 组将是多位代码
- 第 2 组将是标题
- 第 3 组将是描述
- 第 4 组将是电话号码
捕获组
[0][0] = <p>
<b>1628/ SomeBoldedTitle
</b>
Some Description.
Some price 20,00kuna.
<strong>Contact somenumber
098/1234-567 some mail
</
[0][1] = 1628
[0][2] = SomeBoldedTitle
[0][3] = Some Description.
Some price 20,00kuna.
[0][4] = Contact somenumber
098/1234-567 some mail
解释
注意:右击图片并选择在新窗口中查看。
NODE EXPLANATION
----------------------------------------------------------------------
<p> '<p>'
----------------------------------------------------------------------
(?: group, but do not capture (0 or more times
(matching the most amount possible)):
----------------------------------------------------------------------
(?! look ahead to see if there is not:
----------------------------------------------------------------------
<p> '<p>'
----------------------------------------------------------------------
) end of look-ahead
----------------------------------------------------------------------
. any character
----------------------------------------------------------------------
)* end of grouping
----------------------------------------------------------------------
<b> '<b>'
----------------------------------------------------------------------
( group and capture to \1:
----------------------------------------------------------------------
[0-9]+ any character of: '0' to '9' (1 or more
times (matching the most amount
possible))
----------------------------------------------------------------------
) end of \1
----------------------------------------------------------------------
/ '/'
----------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0 or
more times (matching the most amount
possible))
----------------------------------------------------------------------
( group and capture to \2:
----------------------------------------------------------------------
(?: group, but do not capture (0 or more
times (matching the least amount
possible)):
----------------------------------------------------------------------
(?! look ahead to see if there is not:
----------------------------------------------------------------------
</b> '</b>'
----------------------------------------------------------------------
) end of look-ahead
----------------------------------------------------------------------
. any character
----------------------------------------------------------------------
)*? end of grouping
----------------------------------------------------------------------
) end of \2
----------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0 or
more times (matching the most amount
possible))
----------------------------------------------------------------------
</b> '</b>'
----------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0 or
more times (matching the most amount
possible))
----------------------------------------------------------------------
( group and capture to \3:
----------------------------------------------------------------------
(?: group, but do not capture (0 or more
times (matching the least amount
possible)):
----------------------------------------------------------------------
(?! look ahead to see if there is not:
----------------------------------------------------------------------
<strong> '<strong>'
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
<b> '<b>'
----------------------------------------------------------------------
) end of look-ahead
----------------------------------------------------------------------
. any character
----------------------------------------------------------------------
)*? end of grouping
----------------------------------------------------------------------
) end of \3
----------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0 or
more times (matching the most amount
possible))
----------------------------------------------------------------------
< '<'
----------------------------------------------------------------------
(?: group, but do not capture:
----------------------------------------------------------------------
strong 'strong'
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
b 'b'
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
> '>'
----------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0 or
more times (matching the most amount
possible))
----------------------------------------------------------------------
( group and capture to \4:
----------------------------------------------------------------------
(?: group, but do not capture (0 or more
times (matching the least amount
possible)):
----------------------------------------------------------------------
(?! look ahead to see if there is not:
----------------------------------------------------------------------
</ '</'
----------------------------------------------------------------------
) end of look-ahead
----------------------------------------------------------------------
. any character
----------------------------------------------------------------------
)*? end of grouping
----------------------------------------------------------------------
) end of \4
----------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0 or
more times (matching the most amount
possible))
----------------------------------------------------------------------
</ '</'