提问人:ecklerpa 提问时间:2/5/2022 最后编辑:Andy Lesterecklerpa 更新时间:2/5/2022 访问量:39
正则表达式寻找特定的 HTML 标签来标记我正在寻找的字符串的末尾
Regex looking for a specific HTML tag to mark the end of the string I'm looking for
问:
我有一个正则表达式,可以在 HTML 格式的字符串中找到以 'http'、'https' 或 'www'开头的子字符串的开头......
这个正则表达式语句做到了这一点,但从一开始就给了我一切,直到它看到第一次出现空格: \b(?:https?://|www.)\S+\b
是否可以将其修改为停止在特定表达式上,例如,在我的情况下</span>
所以我的 HTML 字符串的格式一致如下:
<div style="font-size:12pt;font-family:Consolas;"><p style="color:#0000FF;font-
size:14pt;margin:0;">This is hilarious:</p><p style="font-family:Consolas;font-
size:9.5pt;margin:0;"><span style="color:#0000FF;font-family:Consolas;font- size:14pt;">https://media.giphy.com/media/lCP95tGSbMmWI/giphy.gif</span></p><p style="color:#0000FF;font-family:"Microsoft Sans Serif";font-size:8.5pt;margin:0;"><br></p></div>
我只想要字符串的返回:https://media.giphy.com/media/lCP95tGSbMmWI/giphy.gif
是否可以修改此正则表达式语句以在看到表达式时停止:
var linkParser = new Regex(@“\b(?:https?://|www.)\S+\b“, 正则表达式选项.已编译 |RegexOptions.IgnoreCase);
答:
1赞
Wiktor Stribiżew
2/5/2022
#1
你可以使用
var linkParser = new Regex(@"\b(?:https?://|www\.)[^\s<>]+", RegexOptions.Compiled | RegexOptions.IgnoreCase);
细节:
\b
- 词边界(?:https?://|www\.)
-http://
或字符串(注意与文字点匹配的转义)https://
www.
.
[^\s<>]+
- 除空格和字符之外的一个或多个字符。<
>
评论