C# Regexp多行

C# Regexp多行,c#,regex,C#,Regex,帮助为这段文本编写正确的regexp StreamReader reader = new StreamReader(opendialog.FileName); string patternPolicy = @"set policy (id)(.+)exit"; var matchesPolicy = Regex.Matches( reader.ReadToEnd(), patternPolicy, RegexOptions.Multiline); 这里需要: Stre

帮助为这段文本编写正确的regexp

StreamReader reader = new StreamReader(opendialog.FileName);
string patternPolicy = @"set policy (id)(.+)exit";
var matchesPolicy = Regex.Matches(
    reader.ReadToEnd(), 
    patternPolicy,
    RegexOptions.Multiline);
这里需要:

StreamReader reader = new StreamReader(opendialog.FileName);
string patternPolicy = @"set policy (id)(.+)exit";
var matchesPolicy = Regex.Matches(
    reader.ReadToEnd(), 
    patternPolicy,
    RegexOptions.Multiline);
匹配所有这些字符串。但关键数字是ID123。 必须覆盖设置策略id 128才能退出

StreamReader reader = new StreamReader(opendialog.FileName);
string patternPolicy = @"set policy (id)(.+)exit";
var matchesPolicy = Regex.Matches(
    reader.ReadToEnd(), 
    patternPolicy,
    RegexOptions.Multiline);
以及如何将每个字符串放入组中,因为我需要将每一行转换为另一种类型的字符串

StreamReader reader = new StreamReader(opendialog.FileName);
string patternPolicy = @"set policy (id)(.+)exit";
var matchesPolicy = Regex.Matches(
    reader.ReadToEnd(), 
    patternPolicy,
    RegexOptions.Multiline);
设置策略id 128 从信任到不信任lm池172.16.2.2/32任何许可 设置策略id 128 设置dst地址MIEP 设置dst地址MIEP WS 设置dst地址半径1 设置dst地址半径2 出口 如何补充我的创作:

StreamReader reader = new StreamReader(opendialog.FileName);
string patternPolicy = @"set policy (id)(.+)exit";
var matchesPolicy = Regex.Matches(
    reader.ReadToEnd(), 
    patternPolicy,
    RegexOptions.Multiline);
您不需要多行选项,需要RegexOptions。单线:

StreamReader reader = new StreamReader(opendialog.FileName);
string patternPolicy = @"set policy (id)(.+)exit";
var matchesPolicy = Regex.Matches(
    reader.ReadToEnd(), 
    patternPolicy,
    RegexOptions.Multiline);
见:

StreamReader reader = new StreamReader(opendialog.FileName);
string patternPolicy = @"set policy (id)(.+)exit";
var matchesPolicy = Regex.Matches(
    reader.ReadToEnd(), 
    patternPolicy,
    RegexOptions.Multiline);
多行 M 使用多行模式,其中^和$匹配每行的开头和结尾,而不是输入字符串的开头和结尾。有关详细信息,请参见多行模式

StreamReader reader = new StreamReader(opendialog.FileName);
string patternPolicy = @"set policy (id)(.+)exit";
var matchesPolicy = Regex.Matches(
    reader.ReadToEnd(), 
    patternPolicy,
    RegexOptions.Multiline);
单线 s 使用单行模式,其中周期为。匹配每个字符,而不是每个字符,但\n。有关详细信息,请参见单线模式

StreamReader reader = new StreamReader(opendialog.FileName);
string patternPolicy = @"set policy (id)(.+)exit";
var matchesPolicy = Regex.Matches(
    reader.ReadToEnd(), 
    patternPolicy,
    RegexOptions.Multiline);
然后,您可能需要通过添加一个?对于你的量词:

StreamReader reader = new StreamReader(opendialog.FileName);
string patternPolicy = @"set policy (id)(.+)exit";
var matchesPolicy = Regex.Matches(
    reader.ReadToEnd(), 
    patternPolicy,
    RegexOptions.Multiline);
string patternPolicy = @"set policy (id)(.+?)exit";
另一件事是,你为什么要把身份证放进一个捕获组?这毫无意义

StreamReader reader = new StreamReader(opendialog.FileName);
string patternPolicy = @"set policy (id)(.+)exit";
var matchesPolicy = Regex.Matches(
    reader.ReadToEnd(), 
    patternPolicy,
    RegexOptions.Multiline);

123号身份证是什么?这里的预期输出是什么?当你说将每个字符串放在一个组中时,你说的是哪些字符串?有一个大的文本文件Creenos config,其中显示了这样不同的文本片段一个属于一个自定义策略配置的行序列。每个策略都有不同的编号id。每个策略配置以exit结尾是否要将set policy id 128转换为exit?如果是这样,你的正则表达式是错误的。谢谢你的回复。看来你是对的!这里需要单线模式。而且模式也是正确的。所以我得到了一组策略配置。现在我需要分别处理每个字符串。必须转换策略组中的每个字符串。请告诉我你有什么想法。这就是我得到的答案。我不明白你的下一个问题在哪里。您将获得结果MatchCollection中的所有匹配项。遍历它,并对匹配的文本执行任何操作