Use regular to delete code _ regular expressions that do not contain a string

Source: Internet
Author: User
First of all, this suspicious ip,58.63.144.170 is said to be a mentally retarded spider, God bless it in hell.

Friends who have seen the Apache log should know that each row of Apache's access log starts with the visitor's IP. Because the log is bigger, so I use UltraEdit to look. When using UltraEdit, it should be noted that there are two kinds of it, one is in line with the Perl specification, and the default is the one with UT, the writing is more special. The regular expressions used in this article are Perl-compliant, and this option can be modified in the Advanced-> configuration-> search-> regular expression engine.

After some thought, I found that the regular expression I mastered did not seem to be able to delete the use of a specified string directly. After thinking about it, write a list that contains the specified string: ^ (58.63.144.170). *$, test, you can find the line containing the specified IP, the next thing to do is how to keep the lines and delete the other rows. After trying n times, I finally gave up the idea of directly deleting rows that do not contain this IP. What do we do? Take a step back to the sky! Since you can't delete it directly, just detour. Although you cannot delete a row that does not contain a specified string, deleting a regular that does not contain the specified character will still be written:

To mark the line containing the specified IP first, use the UT replacement function. The row to find: ^ (58.63.144.170) (. *). Because every line of the log starts with a number, I choose '% ' as the special symbol for the logo, and the regular to be replaced is:%$1$2. When all is replaced, the line containing the specified IP becomes this way:%58.63.144.170 ... Next, we then delete the line that is not '% ', look for: ^[^%].*, the replacement is empty. To perform all the replacements, only the line that begins with ' 58.63.144.170 ' is left in the log. Wait, the rest of the empty line is too ugly, let us kill them, find: ^[\r\n]*$, replace the empty, perform all the replacement, the world finally quiet ...

In fact, the positive in this article are very basic things, although the end can not write the deletion of the line containing the specified string of the regular, but in the roundabout is also the reality of the desired function, and also looks more clear. Sometimes a step back may not be a better choice.

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.