RegEx在HTML标记之间提取文本 [英] RegEx to extract text between a HTML tag
问题描述
我正在寻找一个必须在不同类型的HTML标签之间提取文本的正则表达式。
I'm looking a regular expression which must extract text between HTML tag of different types.
例如:
< span> Span 1< / span>
- O / p: Span 1
<span>Span 1</span>
- O/p: Span 1
< div onclick =callMe()> Span 2< / div>
- O / p: Span 2
<div onclick="callMe()">Span 2</div>
- O/p: Span 2
< a href =#>超文本< / a>
- O / p:超文本
<a href="#">HyperText</a>
- O/p: HyperText
我发现这件特别的作品<([AZ] [A-Z0-9] *)\ b [^>] *>(。*?)< / \1>
来自这里但是这个没有用。
I found this particular piece <([A-Z][A-Z0-9]*)\b[^>]*>(.*?)</\1>
from here But this one is not working.
推荐答案
您的评论显示您忽略了逃避正则表达式字符串中的反斜杠。
Your comment shows that you have neglected to escape the backslashes in your regex string.
如果你想匹配小写字母,请将 az
添加到字符类或使用 Pattern.CASE_INSENSITIVE
(或将(?i)
添加到正则表达式的开头)
And if you want to match lowercase letters add a-z
to the character classes or use Pattern.CASE_INSENSITIVE
(or add (?i)
to the beginning of the regex)
"<([A-Za-z][A-Za-z0-9]*)\\b[^>]*>(.*?)</\\1>"
如果标签内容可能包含换行符,则使用 Pattern.DOTALL
或将(?s)
添加到正则表达式的开头以打开dotall / singleline模式。
If the tag contents may contain newlines, then use Pattern.DOTALL
or add (?s)
to the beginning of the regex to turn on dotall/singleline mode.
这篇关于RegEx在HTML标记之间提取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!