When implementing a complex interface, it often requires a lot of physical labor. For example:
The above is a small part of the method of the interface to be implemented. We need to implement this interface to wrap its handler class, because it is only a package, you only need to transfer all methods to the encapsulated object "getsource ()" for execution.
Fortunately, regular expressions can reduce some work.
However, it is usually difficult to use a regular expression replacement tool to complete such a task. We need to remove comments, obtain method signatures, and forward them to "getsource ()" for execution, it is also necessary to pass the complete parameters over, and it is almost impossible to complete the task using a single regular expression.
Now let's take a look at how RegEx 3 solves this problem:
Step 1 remove comments
Input expression: \ s * //. +
The replacement character is null.
Execute replacement
From the left side, we can see 105 matching comments.
The replacement results show that all annotations are removed:
Step 2: match the entire method body
Select the outputtext node on the left.
Expression: Public (\ s + )(? <Funcname> \ W + )\((? <Parameters> [\ s] *?) \) \ S * {\ s * \ n \ s *(? <Return> return )? [^}] +
No alternative text is entered.
Execute replacement
Step 3 Remove the type declaration characters in the parameter table
Select $ {parameters} node on the left
View matched records through the set record navigation bar
Expression: [\ W \ <\> \ [\] + \ s + (\ W +)
Alternative text: $1
Execute replacement
Select the $ @ {parameters} node on the left, and then view the replaced records through the set record navigation bar.
Step 4 remove line breaks in Parameters
Some too long method parameters are written in multiple rows, removing line breaks to generateCodeMore beautiful.
Expression: [\ r \ n]
The alternative text is empty.
Execute replacement
Select the $ @ {parameters} node on the left side, and view the replaced records through the set record navigation bar.
Step 5 complete the final output
Click outputtext on the left
Alternative text:
Public $1 $ {funcname} ($ {parameters }){
$ {Return} getsource (). $ {funcname} ($ {parameters });
Replace again
Click outputtext at the bottom on the left to view the final result:
Then, copy and paste it back to eclipse:
Conclusion
With the new RegEx 3, you can split the complex matching work into multiple small processes for step-by-step execution. Then, with the innovative $ @ reference symbol function, you can easily write data separately.ProgramCan be completed.