string asd = "<area href='#' title='name' shape='poly' coords='38,23,242'/>"
如何從標題屬性附加傷害提取文本在C#獲取title屬性與正則表達式
,然後插入標題陸續屬性附加傷害?
string asd = "<area href='#' title='name' shape='poly' coords='38,23,242'/>"
如何從標題屬性附加傷害提取文本在C#獲取title屬性與正則表達式
,然後插入標題陸續屬性附加傷害?
搜索:(?<=title=')[^']+
取代:something
演示在這裏:http://regex101.com/r/nR3vQ8
這樣的事情在你的情況:
using System;
using System.Text.RegularExpressions;
class Program
{
static void Main()
{
// This is the input string we are replacing parts from.
string input = "<area href='#' title='name' shape='poly' coords='38,23,242'/>";
// Use Regex.Replace to replace the pattern in the input.
// ... The pattern N.t indicates three letters, N, any character, and t.
string output = Regex.Replace(input, "(?<=title=')[^']+", "something");
// Write the output.
Console.WriteLine(input);
Console.WriteLine(output);
}
}
更新
取出title屬性作爲匹配使用本:
using System;
using System.Text.RegularExpressions;
class Program
{
static void Main()
{
// First we see the input string.
string input = "<area href='#' title='name' shape='poly' coords='38,23,242'/>";
// Here we call Regex.Match.
Match match = Regex.Match(input, @"title='(\w+)'",
RegexOptions.IgnoreCase);
// Here we check the Match instance.
if (match.Success)
{
// Finally, we get the Group value and display it.
string key = match.Groups[1].Value;
Console.WriteLine(key);
}
}
}
輸出
name
試試這個:尤其你可能有興趣在HTMLAgilityPack答案。
Regex reg = new Regex("<a[^>]*?title=\"([^\"]*?\"[^>]*?>");
幾個陷阱的:
這將匹配是大小寫敏感的,你可能需要調整的是
這預計title屬性都存在,並且引述 當然,如果title屬性不存在,你可能不想要匹配呢?
中提取,使用組集合:
reg.Match("<a href=\"#\" title=\"Hello\">Howdy</a>").Groups[1].Value
我需要替換文本拿到冠軍值之後,如何拿到冠軍的價值,。在此先感謝 – Alex
@Alex你在談論哪個價值?你能給我一個例子 – aelor
我需要從標題屬性的價值,..在我的例子中的值是'名稱' – Alex