我遇到一些正则表达式代码的问题,任何人都可以提供帮助。
我有以下数据字段见下文:
abcd " something code " nothing "f <b> cannot find this section </b> "
我想找到"
引号之间的部分。
我可以使用以下regax工作正常:
foreach (Match match in Regex.Matches(sourceLine, @""((\\")|[^"(\\")])+""))
但是,如果引号之间包含<>
的部分找不到该部分。不知道如何在正则表达式中包含<>
标记。
感谢您的时间。
答案 0 :(得分:1)
public List<string> Parse(string input)
{
List<string> results = new List<string>();
bool startSection = true;
int startIndex = 0;
foreach (Match m in Regex.Matches(input, @"(^|[^\\])(")"))
{
if (startSection)
{
startSection = false;
// capture a new section
startIndex = m.Index + """.Length;
}
else
{
// next match starts a new section to capture
startSection = true;
results.Add(input.Substring(startIndex, m.Index - startIndex + 1));
}
}
return results;
}
答案 1 :(得分:0)
character class […]
描述了一组允许的字符,而否定的字符类[^…]
描述了一组不允许的字符。因此[^"(\\")]
表示除&
,q
,u
,o
,t
,;
,{{1}以外的任何字符},(
和\
。除了)
之外,它 不是什么意思。
请改为尝试:
"(")
尽可能少地使用ungreedy quantifier *?
匹配,与尽可能匹配的贪婪量词"(.*?)"
相反。
答案 2 :(得分:0)
您可以使用HttpUtility.HtmlDecode将此文本转换为普通字符。 然后使用正则表达式在双引号之间提取文本很简单。