开发者

How do I match the regular expression pattern within the content of a file without looping through each line in the file?

开发者 https://www.devze.com 2023-01-27 06:39 出处:网络
Im searching for a pattern within a file.This pattern is not limited to a single line.It spreads over more than one line, i.e. more than one line group together to contain this pattern. Hence,开发者_高

Im searching for a pattern within a file. This pattern is not limited to a single line. It spreads over more than one line, i.e. more than one line group together to contain this pattern. Hence,开发者_高级运维 it's not possible to loop through line-by-line in the file and check whether the pattern exists or not. The pattern is given below:

/public.+\s+(\w+)\([^\)]*\)\s*.+?return\s*\w+?\.GetQuestion\s*\(/g

Can anyone please tell the C# coding how to match the pattern with in the file ?


I suspect you need to read the whole file (using ReadToEnd, as suggested by RandomNoob) but then also specify RegexOptions.Singleline to put it into Singleline mode:

The RegexOptions.Singleline option, or the s inline option, causes the regular expression engine to treat the input string as if it consists of a single line. It does this by changing the behavior of the period (.) language element so that it matches every character, instead of matching every character except for the newline character \n or \u000A.

Regex pattern = new Regex(
    @"public.+\s+(\w+)\([^\)]*\)\s*.+?return\s*\w+?\.GetQuestion\s*\(",
    RegexOptions.Singleline);


Use StreamReader ReadToEnd and match against it?

0

精彩评论

暂无评论...
验证码 换一张
取 消