RegEx 3 Regular Expression practice tutorial -- interface packaging in the decorator mode in eclipse

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.