How to match a method block using regex?

前端 未结 7 1299
孤街浪徒
孤街浪徒 2021-01-06 10:06

Take an example.

 public static FieldsConfig getFieldsConfig(){
    if(xxx) {
      sssss;
    }
   return;
}

I write a regex, \"\\\\

7条回答
  •  甜味超标
    2021-01-06 10:36

    Regex is definitely not the best tool for that, but if you want regex, and your code is well indented, you can try with:

    ^(?\s*)(?\w+)\s(?\w+)?\s*(?\w+)?\s*(?\b\w+)\s(?\w+)\((?.*?)\)\s*\{(?.+?)^\k\}
    

    DEMO

    It has additional named groups, you can delete them. It use a indentation level to find last }.

提交回复
热议问题