简体   繁体   English

正则表达式模式以匹配C#中的if语句?

[英]Regular Expression pattern to match if statement in C#?

I am using the following Regular Expression pattern to match an if statement written in C# style; 我正在使用以下正则表达式模式来匹配以C#风格编写的if语句;

\b[if]{2}\b[ ]?\({1}(?<HeaderSection>[ \w\s\a\t\=\.\@\#\$\%\&a-zA-Z0-9\(\)\;\/\"\'\[\]\*]*)\){1}(?<CommentSection>[\s\a\w\t a-zA-Z0-9\/\.]*)[\r\n]*\{{1}(?<FunctionBody>[\r\n \a\s\wa-zA-Z0-9\(\)\"\.\;\:]*)[\r\n]*\}{1}

Its a crazy long regex pattern but seems to be working to some extent.Let me explain it,it has three named capturing Groups namely HeaderSection , CommentSection and FunctionBody .HeaderSection captures match between starting and closing parentheses of if statement,such as from the statement below; 它是一个疯狂的长正则表达式模式,但似乎在一定程度上起作用。让我解释一下,它具有三个命名的捕获组,分别是HeaderSectionCommentSectionFunctionBody。HeaderSection捕获if语句的开始和结束括号之间的匹配,例如从语句下面;

if(Value1==Function(int Z))

it captures ; 它捕获;

Value1==Function(int Z)

Similarly CommentSection captures comment(if any) after the closing parentheses,so from the statement below ; 同样,CommentSection在右括号后捕获注释(如果有),因此从下面的语句中;

if(Value1==Function(int Z))//This is a Comment.

it captures 它捕获

//This is a Comment.

and FunctionBody captures anything between { and },such as in the code below ; 和FunctionBody捕获{和}之间的任何内容,例如下面的代码;

if(Value1==Function(int Z))//This is a Comment.
{
  This is the
  space for
  function body.
}

it captures "This is the space for function body." 它捕获“这是功能主体的空间”。 So that was explanation of what the regex matches.Now the issue with it is that if i have some function like this; 所以这就是正则表达式匹配的解释。现在的问题是,如果我有这样的功能;

if(Value1==Function(int Z)//This is a Comment.
{
  if(Value2==Value1)
  {
    Some code
  }
}

and if i match it using the regex above it doesn't match the first if declaration ie; 如果我使用上面的正则表达式匹配它,则它与第一个if声明不匹配,即;

if(Value1==Function(int Z)//This is a Comment.
{
Another function();
}

and instead matches the inner one ie 而是匹配内部的即

  if(Value2==Value1)
  {
    Some code
  }

Please point what i have done wrong,or if there is another way that is less messy please let me know,or correct the regex pattern if its wrong somewhere.One more thing i'm doing all this in C# using Regular Expression functions. 请指出我做错了什么,或者如果还有其他方法可以减少混乱,请让我知道,或者在某处出错时纠正正则表达式模式。还有另一件事,我正在使用正则表达式函数在C#中完成所有这些工作。 Thanks in advance. 提前致谢。

(?<header>if\(.*?)(?<comment>//.*?)*\s\n\{(?<functionbody>.*?)\n\}

this seems to be a solution if the paran is formated in the supposed way. 如果以假定的方式格式化Paran,这似乎是一个解决方案。

(?<header>if\(.*?)

will match if( followed by anything BUT before the // section, so it will match 将匹配if(后跟//部分之前的任何BUT,因此它将匹配

if(Value1==Function(int Z))

then it moves on to the (?<comment>//.*?)*\\s that will match anything following the // signs BUT will also match if there is nothing * equals zero or more occurences, and the \\s makes sure that it doesnt go beyond the line end. 然后它移动到(?<comment>//.*?)*\\s ,它将匹配//符号后面的任何内容,但如果没有*等于零或更多次出现,BUT也将匹配,并且\\s确保它不会超出行尾。

then (\\n\\{)(?<functionbody>.*?)(\\n\\}) matches any { just after a newline and progresses until a } is found just after a newline. 然后(\\n\\{)(?<functionbody>.*?)(\\n\\})与任何{匹配,仅在换行符之后进行,直到在换行符之后找到}

in

var x = 0
if(Value1==Function(int Z))//This is a Comment.
{
  if(Value2==Value1)
  {
    Some code
  }
}
var y = 0

if(y == x) 
{
    x = y + 1
}

it will match the following groups : 它将匹配以下组:

header: if(Value1==Function(int Z))
comment: //This is a Comment.
functionbody: 
  if(Value2==Value1)
  {
    Some code
  }

header: if(y == x) 
functionbody: 
        x = y + 1

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

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