网站首页 > 知识剖析 正文
我们今天来分析解释一下这个表达式string hrefPattern = @"href\s*=\s*(?:""'[""']|(?<1>[^>\s]+))";,并用实例演示用法。这个正则表达式用于从文本中提取href属性的值,这些值可以是被单引号或双引号包围的,或者是不包含大于符号和空白字符的文本。我们分解这个正则表达式来详细解释它的各个部分:
1. href\s*=\s*: 这部分匹配 href 关键字,后面可以跟着零个或多个空白字符,然后是一个等号,再然后又是零个或多个空白字符。其中href: 直接匹配文本中的"href",这是HTML中表示链接地址的属性名称。\s*=\s*: 匹配等号(=),等号前后可以有0个或多个空白字符(包括空格、制表符、换行符等)。
2. (?:...): 这是一个非捕获组,意味着它会匹配括号内的内容,但不会为其创建一个捕获组。这意味着我们不能直接从匹配结果中提取这部分内容。
3. [""'](?<1>[^""']*)[""']: 这部分匹配被单引号或双引号包围的任何内容。具体来说:
1. [""']: 匹配一个单引号或双引号。
2. (?<1>[^\"']*): 创建了一个命名捕获组,名为1,用来捕获在引号之间的任何非引号字符序列,这就是href属性的值。(?<1>...): 这是一个命名捕获组,但这里它被放在了一个非捕获组内,这意味着它不会捕获匹配的内容。
3. [^""']*: 匹配任何不是单引号或双引号的字符零次或多次。
4. [""']: 再次匹配一个单引号或双引号。
4. |: 或者操作符,表示前面的模式和后面的模式中的任何一个可以匹配。又叫管道符号,代表逻辑“或”操作,也就是表示前面的模式与后面的模式任一满足即可。
5. (?<1>[^>\s]+): 这部分匹配任何不是大于符号或空白字符的字符一次或多次。这也是一个命名捕获组,但同样,它被放在了一个非捕获组内。当href值没有被引号包围时使用。也就是这部分匹配不是大于符号(>)和空白字符的任何字符1次或多次,但不包括引号。
综上所述,此正则表达式能够处理以下两种格式的href属性及其值:
1. 被引号包围的情况:<a href="http://example.com">...</a> 或 <a href='http://example.com'>...</a>
2. 未被引号包围的情况:<a href=http://example.com>...</a>
实例演示用法:
using System.Text.RegularExpressions;
namespace ConsoleAppC
{
internal class Program
{
static void Main(string[] args)
{
string inputString = @"<a href=""http://example.com"">Link</a>
<a href='http://another.example.com'>Another Link</a>
<a href=http://noquotes.example.com>No Quotes Link</a>";
string hrefPattern = @"href\s*=\s*(?:[""'](?<1>[^""']*)[""']|(?<1>[^>\s]+))";
MatchCollection matches = Regex.Matches(inputString, hrefPattern);
foreach (Match match in matches)
{
Console.WriteLine(match.Value); // 输出匹配到的href属性值
Console.WriteLine(#34;Found href value: {match.Groups[1].Value} at index: {match.Groups[1].Index}");
}
}
}
}
运行这段代码后,将输出如下结果:
href="http://example.com"
Found href value: http://example.com at index: 9
href='http://another.example.com'
Found href value: http://another.example.com at index: 72
href=http://noquotes.example.com
Found href value: http://noquotes.example.com at index: 150
为了给大家演示如何使用这个正则表达式,我们再看以下例子:
假设我们有以下的HTML片段:
<a href="https://www.example.com">Click here</a>
<a href='https://www.example.org'>Go there</a>
<a href="https://www.example.net" target="_blank">Open external link</a>
使用上述的正则表达式,我们可以提取所有的href属性值:
string input = @"<a href=\""https://www.example.com\"">Click here</a>
<a href='https://www.example.org'>Go there</a>
<a href=\""https://www.example.net\"" target=\""_blank\"">Open external link</a>";
代码为:
string hrefPattern = @"href\s*=\s*(?:[""'](?<1>[^""']*)[""']|(?<1>[^>\s]+))";
Regex regex = new Regex(hrefPattern, RegexOptions.IgnoreCase | RegexOptions.Compiled);
MatchCollection matches = regex.Matches(input);
foreach (Match match in matches)
{
Console.WriteLine(#34;Found href: {match.Groups["1"].Value}");
}
string input = @"<a href=\""https://www.example.com\"">Click here</a>
输出将是:
Found href: \"https://www.example.com\"
Found href: https://www.example.org
Found href: \"https://www.example.net\"
注意,这个正则表达式并不完美,它可能无法处理所有可能的HTML格式,但对于简单的用途来说可能已经足够了。
猜你喜欢
- 2024-11-17 微信公众号发送客服消息---菜单消息,卡片消息
- 2024-11-17 4.页面结构(页面结构元素)
- 2024-11-17 JavaScript代码的三种引入方式【操作演示】
- 2024-11-17 我们为什么要使用 href="javascript:void(0)"
- 2024-11-17 一、学爬虫前,你需要知道的爬虫常识
- 2024-11-17 我只用一个txt文档,就将公众号里面的所有文章列表提取出来
- 2024-11-17 码畜在工作中总结的知识点,记录一下
- 2024-11-17 04元素类型(元素种类总结)
- 2024-11-17 H5 nav元素的作用&应用场合(互联网应用与北斗系统在其中的作用)
- 2024-11-17 2024 年 PWA 无法与 Native 媲美的 7 个特性
- 04-29php开发者composer使用看这一篇就够了
- 04-29引用和变量声明在不同语言中的实作
- 04-29PHP 没你想的那么差
- 04-29Ubuntu linux 上的 Nginx 和 Php 安装
- 04-29CentOS下通过yum搭建lnmp(单版本PHP)
- 04-29为什么 PHP8 是个高性能版本
- 04-29PHP8函数包含文件-PHP8知识详解
- 04-29使用无参数函数进行命令执行
- 最近发表
- 标签列表
-
- xml (46)
- css animation (57)
- array_slice (60)
- htmlspecialchars (54)
- position: absolute (54)
- datediff函数 (47)
- array_pop (49)
- jsmap (52)
- toggleclass (43)
- console.time (63)
- .sql (41)
- ahref (40)
- js json.parse (59)
- html复选框 (60)
- css 透明 (44)
- css 颜色 (47)
- php replace (41)
- css nth-child (48)
- min-height (40)
- xml schema (44)
- css 最后一个元素 (46)
- location.origin (44)
- table border (49)
- html tr (40)
- video controls (49)