Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 正则表达式选项匹配多行并忽略大小写_C#_Html_Regex_Multiline_Ignore Case - Fatal编程技术网

C# 正则表达式选项匹配多行并忽略大小写

C# 正则表达式选项匹配多行并忽略大小写,c#,html,regex,multiline,ignore-case,C#,Html,Regex,Multiline,Ignore Case,我有一段格式不正确的html,有时缺少“。此外,它有时显示大写字母,有时显示小写字母: <DIV class="main"> <DIV class="subsection1"> <H2> <DIV class=subwithoutquote>StackOverflow</DIV></H2></DIV></DIV> 堆栈溢出 我想同时匹配多行和忽略大小写。但是下面的模式似乎不起

我有一段格式不正确的html,有时缺少“。此外,它有时显示大写字母,有时显示小写字母:

<DIV class="main">
    <DIV class="subsection1">
   <H2>
   <DIV class=subwithoutquote>StackOverflow</DIV></H2></DIV></DIV>

堆栈溢出
我想同时匹配多行和忽略大小写。但是下面的模式似乎不起作用。(对于串联,我也尝试了|而不是&)

const字符串模式=@“(.+?)”;
Match m=Regex.Match(html,pattern,RegexOptions.IgnoreCase&RegexOptions.Singleline);

或者我应该在模式中添加\n*来解决多行问题吗?

第一个问题是您不允许在选项卡之间的正则表达式中使用空格。正确的正则表达式(在Rubular中测试)是:

由于这些是位标志,按位And(
&
运算符)是错误的标志。您需要的是按位Or(
运算符)

按位And表示“如果位设置在这两个中,则保持设置状态;否则,将其取消设置。您需要按位或,这意味着“如果位设置在这些中的中,则设置它;否则,取消设置它。”


在这种情况下,您需要将它们放在一起

const string pattern = @"<div class=""?main""?><div class=""?subsection1""?><h2><div class=""?subwithoutquote""?>(.+?)</div>";
Match m = Regex.Match(html, pattern, RegexOptions.IgnoreCase | RegexOptions.Singleline)
const字符串模式=@“(.+?)”;
Match m=Regex.Match(html、模式、RegexOptions.IgnoreCase | RegexOptions.Singleline)
编辑:将正则表达式更改为以下内容

const string pattern = @"<div class="?main"?>\s*<div class="?subsection1"?>\*+<h2>\s*<div class="?subwithoutquote"?>(.+?)</div>
常量字符串模式=@“\s*\*+\s*(.+?)
我已经编辑了您的标题。请参见“”,其中的共识是“不,他们不应该”“。你试过使用HTML解析器吗?你的正则表达式失败了,因为你不允许在标记之间使用空格。
\s+
。我已经编辑了我的原始文章。我尝试了这两种方法&而且两种方法都不起作用。@Yang我已经更新并更正了空格的正则表达式,解决了这个问题。
Match m = Regex.Match(html, pattern, RegexOptions.IgnoreCase & RegexOptions.Singleline);
const string pattern = @"<div class=""?main""?><div class=""?subsection1""?><h2><div class=""?subwithoutquote""?>(.+?)</div>";
Match m = Regex.Match(html, pattern, RegexOptions.IgnoreCase | RegexOptions.Singleline)
const string pattern = @"<div class="?main"?>\s*<div class="?subsection1"?>\*+<h2>\s*<div class="?subwithoutquote"?>(.+?)</div>