繁体   English   中英

匹配模式到行尾

[英]Matching pattern to end of line

我正在尝试从文本文件中获取一些行内注释,并且需要一些有关表达式的帮助。

this comes before selection
this is on the same line %% this is the first group and it can have any character /[{3$5!+-p
here is some more text in the middle
this stuff is also on a line with a comment %% this is the second group of stuff !@#%^()<>/~`
this goes after the selections

我正在尝试获取%% \\ s +之后的所有内容。 这是我尝试过的:

%% \\ s +(。*)$

但这与第一个%%之后的所有文本匹配。 不知道从这里去哪里。

大多数引擎默认为点与换行符不匹配
AND不是多行模式。

这意味着%%\\s+(.*)$不匹配,除非找到
字符串最后一行的%%

与其尝试对抗,不如使用内联修饰符(?..)
覆盖外部开关。

使用(?-s)%%\\s+(.*)起飞点的所有

由于. 默认情况下匹配除换行符以外的任何字符,您无需使用$

%%\s+(.*)

正则表达式演示

说明:

  • %% -两个文字%符号
  • \\s+ -1个或多个空格
  • (.*) -除换行符外的0个或多个其他字符(捕获到组1中)

在此处输入图片说明

C#演示

var s = "THE_STRING";
var result = Regex.Matches(s, @"%%\s+(.*)")
            .Cast<Match>()
            .Select(p=>p.Groups[1].Value)
            .ToList();
Console.WriteLine(string.Join("\n", result));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM