I have a line of code:
我有一行代码:
<link href="<%= Page.ResolveClientUrl("~/Styles/CAR.css") %>" rel="stylesheet" type="text/css" />
I just want to extract ~/Styles/CAR.css
from it. Kindly let me know Regex for this. link href tag may contain other syntax as well to refer css. For Ex, <link href="<%= Url.Content("~/Styles/CAR.css") %>" rel="stylesheet" type="text/css" />
我只想从中提取〜/ Styles / CAR.css。请让我知道正则表达式。 link href标签也可以包含其他语法来引用css。对于Ex, ”rel =“stylesheet”type =“text / css”/>
4 个解决方案
#1
1
Besides that you should'nt parse HTML with regex, I'd go for
除此之外你不应该用正则表达式解析HTML,我会去
\(\"(.+)\"\)
as your regex. Simply extract anything between ("
and ")
.
作为你的正则表达式。只需在(“和”)之间提取任何内容。
For example:
例如:
string strRegex = @"\(\""(.+)\""\)";
Regex myRegex = new Regex(strRegex, RegexOptions.None);
string strTargetString = @"<link href=""<%= Page.ResolveClientUrl(""~/Styles/CAR.css"") %>"" rel=""stylesheet"" type=""text/css"" />";
foreach (Match myMatch in myRegex.Matches(strTargetString))
{
if (myMatch.Success)
{
// Add your code here
}
}
(example code taken from http://regexhero.net/tester/)
(示例代码来自http://regexhero.net/tester/)
If there will be only one occurence of <link href=""<%= Page.ResolveClientUrl(""~/Styles/CAR.css"") %>"" rel=""stylesheet"" type=""text/css"" />
or you want to get only the first occurence, then you can get rid of the for-loop and use:
如果只出现 “”rel =“”stylesheet“”type =“”text / css“”/>或者你想只获得第一次出现,那么你可以摆脱for循环并使用:
string strRegex = @"\(\""(.+)\""\)";
Regex myRegex = new Regex(strRegex, RegexOptions.None);
string strTargetString = @"<link href=""<%= Page.ResolveClientUrl(""~/Styles/CAR.css"") %>"" rel=""stylesheet"" type=""text/css"" />";
Match myMatch = myRegex.Match(strTargetString);
The difference here is using Regex.Matches(string)
(which returns a MatchCollection
; every matched occurence) vs Regex.Match(string)
(which returns a single Match
; the first matched occurence only).
这里的区别在于使用Regex.Matches(字符串)(返回MatchCollection;每个匹配的出现)vs Regex.Match(字符串)(返回单个匹配;仅第一个匹配的出现)。
#2
2
I suggest you to use HtmlAgilityPack (available from NuGet) for HTML parsing. Getting href
attribute value will look like:
我建议你使用HtmlAgilityPack(可从NuGet获得)进行HTML解析。获取href属性值将如下所示:
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(stringWithHtml);
var link = doc.DocumentNode.SelectSingleNode("//link[@href]");
var href = link.Attributes["href"].Value;
Then you can extract ~/Styles/CAR.css
from content of attribute. Regex is good here, but you also can avoid it:
然后你可以从属性的内容中提取〜/ Styles / CAR.css。正则表达式在这里很好,但你也可以避免它:
int startIndex = href.IndexOf('"');
int endIndex = href.LastIndexOf('"');
var result = href.Substring(startIndex + 1, endIndex - startIndex - 1);
// ~/Styles/CAR.css
Extracting path with regex will look like
用正则表达式提取路径看起来像
var match = Regex.Match(href, @"ResolveClientUrl\(""(.*)""\)");
if (match.Success)
result = match.Groups[1].Value;
#3
0
use this:
用这个:
/\(([^\)]*)\)/
Tested with perl:
用perl测试:
> cat temp
<link href="<%= Page.ResolveClientUrl("~/Styles/CAR.css") %>" rel="stylesheet" type="text/css" />
> perl -lne 'print $1 if(/\(([^\)]*)\)/)' temp
"~/Styles/CAR.css"
>
#4
0
<link href="<%= Page.ResolveClientUrl("~/Styles/CAR.css") %>" rel="stylesheet" type="text/css" />
”rel =“stylesheet”type =“text / css”/>
For quick regex we can use the information inside the quotes inside parentheses (("~/Styles/CAR.css") ) and use that info to group it into one one.
对于快速正则表达式,我们可以使用括号内的引号内的信息((“〜/ Styles / CAR.css”))并使用该信息将其分组为一个。
Escaping the parentheses one quick regex would be
逃避括号一个快速正则表达式
<link href="<%=.*\("(.*)\).*%>"(.*/>)
“(。* />)
In the above regex there are two groups. The first matched group would give us the required information i.e. ~/Styles/CAR.css.
在上面的正则表达式中有两组。第一个匹配的组将向我们提供所需的信息,即〜/ Styles / CAR.css。
You can check it in http://regexpal.com/ and experiment with other patterns.
您可以在http://regexpal.com/中查看它并尝试其他模式。
#1
1
Besides that you should'nt parse HTML with regex, I'd go for
除此之外你不应该用正则表达式解析HTML,我会去
\(\"(.+)\"\)
as your regex. Simply extract anything between ("
and ")
.
作为你的正则表达式。只需在(“和”)之间提取任何内容。
For example:
例如:
string strRegex = @"\(\""(.+)\""\)";
Regex myRegex = new Regex(strRegex, RegexOptions.None);
string strTargetString = @"<link href=""<%= Page.ResolveClientUrl(""~/Styles/CAR.css"") %>"" rel=""stylesheet"" type=""text/css"" />";
foreach (Match myMatch in myRegex.Matches(strTargetString))
{
if (myMatch.Success)
{
// Add your code here
}
}
(example code taken from http://regexhero.net/tester/)
(示例代码来自http://regexhero.net/tester/)
If there will be only one occurence of <link href=""<%= Page.ResolveClientUrl(""~/Styles/CAR.css"") %>"" rel=""stylesheet"" type=""text/css"" />
or you want to get only the first occurence, then you can get rid of the for-loop and use:
如果只出现 “”rel =“”stylesheet“”type =“”text / css“”/>或者你想只获得第一次出现,那么你可以摆脱for循环并使用:
string strRegex = @"\(\""(.+)\""\)";
Regex myRegex = new Regex(strRegex, RegexOptions.None);
string strTargetString = @"<link href=""<%= Page.ResolveClientUrl(""~/Styles/CAR.css"") %>"" rel=""stylesheet"" type=""text/css"" />";
Match myMatch = myRegex.Match(strTargetString);
The difference here is using Regex.Matches(string)
(which returns a MatchCollection
; every matched occurence) vs Regex.Match(string)
(which returns a single Match
; the first matched occurence only).
这里的区别在于使用Regex.Matches(字符串)(返回MatchCollection;每个匹配的出现)vs Regex.Match(字符串)(返回单个匹配;仅第一个匹配的出现)。
#2
2
I suggest you to use HtmlAgilityPack (available from NuGet) for HTML parsing. Getting href
attribute value will look like:
我建议你使用HtmlAgilityPack(可从NuGet获得)进行HTML解析。获取href属性值将如下所示:
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(stringWithHtml);
var link = doc.DocumentNode.SelectSingleNode("//link[@href]");
var href = link.Attributes["href"].Value;
Then you can extract ~/Styles/CAR.css
from content of attribute. Regex is good here, but you also can avoid it:
然后你可以从属性的内容中提取〜/ Styles / CAR.css。正则表达式在这里很好,但你也可以避免它:
int startIndex = href.IndexOf('"');
int endIndex = href.LastIndexOf('"');
var result = href.Substring(startIndex + 1, endIndex - startIndex - 1);
// ~/Styles/CAR.css
Extracting path with regex will look like
用正则表达式提取路径看起来像
var match = Regex.Match(href, @"ResolveClientUrl\(""(.*)""\)");
if (match.Success)
result = match.Groups[1].Value;
#3
0
use this:
用这个:
/\(([^\)]*)\)/
Tested with perl:
用perl测试:
> cat temp
<link href="<%= Page.ResolveClientUrl("~/Styles/CAR.css") %>" rel="stylesheet" type="text/css" />
> perl -lne 'print $1 if(/\(([^\)]*)\)/)' temp
"~/Styles/CAR.css"
>
#4
0
<link href="<%= Page.ResolveClientUrl("~/Styles/CAR.css") %>" rel="stylesheet" type="text/css" />
”rel =“stylesheet”type =“text / css”/>
For quick regex we can use the information inside the quotes inside parentheses (("~/Styles/CAR.css") ) and use that info to group it into one one.
对于快速正则表达式,我们可以使用括号内的引号内的信息((“〜/ Styles / CAR.css”))并使用该信息将其分组为一个。
Escaping the parentheses one quick regex would be
逃避括号一个快速正则表达式
<link href="<%=.*\("(.*)\).*%>"(.*/>)
“(。* />)
In the above regex there are two groups. The first matched group would give us the required information i.e. ~/Styles/CAR.css.
在上面的正则表达式中有两组。第一个匹配的组将向我们提供所需的信息,即〜/ Styles / CAR.css。
You can check it in http://regexpal.com/ and experiment with other patterns.
您可以在http://regexpal.com/中查看它并尝试其他模式。