Could some one help me out by providing the regex to detect a pattern in C#? The input string would be of the type-
有人可以通过提供正则表达式来检测C#中的模式吗?输入字符串的类型是 -
<p><someURL></p>
I want to check whether the input has the content - <someURL> (with the angular bracket). So I need a regex to detect that
我想检查输入是否具有内容 -
2 个解决方案
#1
You can obtain the <URL>
part between any <p>
/</p>
tags by using
您可以使用在
/
标记之间获取
var rxx = new Regex(@"</?p\b[^<]*>");
var reslt = rxx.Split("<p><someURL></p>")[1];
Output:
Mind that in case you have other tags, you will need to modify </?p\b[^<]*>
regex. Also, if there are more tags, you will need to use Match
:
请注意,如果您有其他标签,则需要修改 正则表达式。此外,如果有更多标签,您将需要使用匹配:
rxx = new Regex(@"(?<=<p\b[^<]*>).*?(?=</p>)");
var reslt2 = rxx.Matches("<p><someURL></p><p><anotherURL></p>").Cast<Match>().ToList();
Output:
In case you have to deal with entire HTML/XML/SGML/ML and other .*ML texts, HtmlAgilityPack is the best way to go.
如果您必须处理整个HTML / XML / SGML / ML和其他。* ML文本,HtmlAgilityPack是最好的方法。
#2
Regex Pattern :-
正则表达式: -
\<(.*?)>
This will yield the groups of the text in between the angle brackets. And then in the foreach loop, you can retrieve the element text for the desired elements.
这将产生尖括号之间的文本组。然后在foreach循环中,您可以检索所需元素的元素文本。
Sample - http://regexr.com/3aufj
示例 - http://regexr.com/3aufj
OR
Use https://htmlagilitypack.codeplex.com/ to parse the html string into an object and navigate within the structure on the server side.
使用https://htmlagilitypack.codeplex.com/将html字符串解析为对象,并在服务器端的结构中导航。
#1
You can obtain the <URL>
part between any <p>
/</p>
tags by using
您可以使用在
/
标记之间获取
var rxx = new Regex(@"</?p\b[^<]*>");
var reslt = rxx.Split("<p><someURL></p>")[1];
Output:
Mind that in case you have other tags, you will need to modify </?p\b[^<]*>
regex. Also, if there are more tags, you will need to use Match
:
请注意,如果您有其他标签,则需要修改 正则表达式。此外,如果有更多标签,您将需要使用匹配:
rxx = new Regex(@"(?<=<p\b[^<]*>).*?(?=</p>)");
var reslt2 = rxx.Matches("<p><someURL></p><p><anotherURL></p>").Cast<Match>().ToList();
Output:
In case you have to deal with entire HTML/XML/SGML/ML and other .*ML texts, HtmlAgilityPack is the best way to go.
如果您必须处理整个HTML / XML / SGML / ML和其他。* ML文本,HtmlAgilityPack是最好的方法。
#2
Regex Pattern :-
正则表达式: -
\<(.*?)>
This will yield the groups of the text in between the angle brackets. And then in the foreach loop, you can retrieve the element text for the desired elements.
这将产生尖括号之间的文本组。然后在foreach循环中,您可以检索所需元素的元素文本。
Sample - http://regexr.com/3aufj
示例 - http://regexr.com/3aufj
OR
Use https://htmlagilitypack.codeplex.com/ to parse the html string into an object and navigate within the structure on the server side.
使用https://htmlagilitypack.codeplex.com/将html字符串解析为对象,并在服务器端的结构中导航。