推荐文章

c#去除字符串中的空格,回车,换行符,制表符

c#去除字符串中的空格,回车,换行符,制表符

c#去除字符串中的空格,回车,换行符,制表符
C#监控程序启动和关闭

C#监控程序启动和关闭

C#监控程序启动和关闭using System;using System.Collections.Generic;using System.Text;using System.Diagnostics;using System.Threading;namespace ProcessListener{ class Program { static void Main(stri
C#操作USB口的摄像头

C#操作USB口的摄像头

private const uint BM_CLICK = 0xF5; 鼠标点击的消息,对于各种消息的数值,查API手册,也可用VS2010自带的SPY++ [DllImpt("user32.dll", EntryPoint = "SendMessage", SetLastErr = true, Set = Set.Auto)]private static extern int SendMe
C# FTP操作

C# FTP操作

C# FTP操作
C#文件监控对象FileSystemWatcher

C#文件监控对象FileSystemWatcher

使用C#文件监控对象FileSystemWatcher对文件夹下的删除、修改、新增

C#用正则表达式 获取标签的属性或值

日期:2019-03-15 点击:3015 来源:PB2.CN

1、获取标签中的值: <a href="www.pb2.cn" class="main" >PB2</a> 结果:PB2

var str="<a href=\"www.pb2.cn\" class=\"main\" >PB2</a>";
var title="a";
var result= GetTitleContent( str,   title);//结果:PB2

/// <summary>  
/// 获取字符中指定标签的值  
/// </summary>  
/// <param name="str">字符串</param>  
/// <param name="title">标签</param>  
/// <returns>值</returns>  
public static string GetTitleContent(string str, string title)  
{  
    string tmpStr = string.Format("<{0}[^>]*?>(?<Text>[^<]*)</{1}>", title, title); //获取<title>之间内容  

    Match TitleMatch = Regex.Match(str, tmpStr, RegexOptions.IgnoreCase);  

    string result = TitleMatch.Groups["Text"].Value;  
    return result;  
}


 
2、获取标签中的属性:<a href="www.pb2.cn" class="main">PB2</a>  获取 “href” 的结果:www.pb2.cn

var str="<a href=\"www.pb2.cn\" class=\"main\" >PB2</a>";
var title="a";
var attrib="href"
var result= GetTitleContent( str,title,attrib);//结果:www.pb2.cn

/// <summary>  
/// 获取字符中指定标签的值  
/// </summary>  
/// <param name="str">字符串</param>  
/// <param name="title">标签</param>  
/// <param name="attrib">属性名</param>  
/// <returns>属性</returns>  
public static string GetTitleContent(string str, string title,string attrib)  
{  

    string tmpStr = string.Format("<{0}[^>]*?{1}=(['\"\"]?)(?<url>[^'\"\"\\s>]+)\\1[^>]*>", title, attrib); //获取<title>之间内容  

    Match TitleMatch = Regex.Match(str, tmpStr, RegexOptions.IgnoreCase);  

    string result = TitleMatch.Groups["url"].Value;  
    return result;  
}

 

这篇文档对您是否有帮助?

c#去除字符串中的空格,回车,换行符,制表符

c#去除字符串中的空格,回车,换行符,制表符

c#去除字符串中的空格,回车,换行符,制表符
C#监控程序启动和关闭

C#监控程序启动和关闭

C#监控程序启动和关闭using System;using System.Collections.Generic;using System.Text;using System.Diagnostics;using System.Threading;namespace ProcessListener{ class Program { static void Main(stri
C#操作USB口的摄像头

C#操作USB口的摄像头

private const uint BM_CLICK = 0xF5; 鼠标点击的消息,对于各种消息的数值,查API手册,也可用VS2010自带的SPY++ [DllImpt("user32.dll", EntryPoint = "SendMessage", SetLastErr = true, Set = Set.Auto)]private static extern int SendMe
C# FTP操作

C# FTP操作

C# FTP操作
C#文件监控对象FileSystemWatcher

C#文件监控对象FileSystemWatcher

使用C#文件监控对象FileSystemWatcher对文件夹下的删除、修改、新增