Java ---- use regular expressions to match keywords in Java source code, java ---- Regular Expressions

Source: Internet
Author: User

Java ---- use regular expressions to match keywords in Java source code, java ---- Regular Expressions

Write this blog to learn the regular expressions in Java systematically. Another reason is that there was a problem that could not be solved. I had a reverse match and I couldn't stand it myself. However, as a code monkey, I should not solve it in this way. It is impossible in Java. the method I want. (If not, I will immediately transfer to Cshit .)

Let's take a look at the problems I encountered at the beginning.

In the past, a front-end sister sent a time data type to the backend. However, what she sent to me was in the following format:, January 1, August 18, 2017.

At that time, I was forced to see this format, and Baidu had not found a solution for a long time. I cannot convert this String to Date.

So I thought of using a regular expression to obtain numbers in the String. (2017, 08, 18, 15, 41)

Then, a new Date data type is stored in the database.

I think this is good, but it is really cruel to show.

However, I found a split method in the Pattern class, which is a splitter.

For example, I write regular expressions to match numbers, but the splitter splits the numbers. ["Year", "month", "day", "Hour", "Minute"]

I don't need any numbers, so I thought of a reverse matching routine, matching non-numeric characters (strings ).

After talking a lot about the code, let's get a reverse matching code first.

1 Pattern pattern = Pattern. compile ("[^ 0-9] +"); 2 String [] strings = pattern. split ("August 18, 2017"); 3 System. out. println (Arrays. toString (strings ));

Running result

This type of reverse matching can only be applied to simple matching. If it is complicated, it cannot be written.

So, in the moonlight of a night, I am sore and happy. So I decided to study the system and see if there are any methods I want in Java.

Reference blog: http://www.cnblogs.com/ggjucheng/p/3423731.html

Add a connection: http://www.kaiyuanba.cn/html/1/131/138/7609.htm

Attached: Regular Expression 30-minute getting started tutorial

I actually learned this method after reading that blog:

1 Pattern p = Pattern. compile ("\ d +"); 2 Matcher m = p. matcher ("My QQ is: 456456 my phone is: 0532214 my mailbox is: aaa123@aaa.com"); 3 while (m. find () {4 System. out. println (m. group (); 5}

This method of finding and group is similar to the hashNext and next methods in the iterator.

 

Finally, let's go to the code. Otherwise, the entire blog will be out of question.

Question: Count keywords in Java source code. If the keywords are in comments or strings, no statistics are collected.

1 package setAndMap_Exe; 2 3 import java. io. file; 4 import java. util. map; 5 import java. util. dependencies; 6 import java. util. set; 7 import java. util. treeMap; 8 import java. util. regex. matcher; 9 import java. util. regex. pattern; 10 11 public class Exe3 {12 public static Map <String, Integer> map = new TreeMap <> (); 13 14 public static void main (String [] args) throws Exception {15 File file = new File ("input. Txt "); 16 String sb =" "; 17 bytes input = new bytes (file); 18 while (input. hasNextLine () {19 sb + = input. nextLine () + "\ n"; 20} 21 input. close (); 22 23 // remove comment space line break 24 Pattern pattern1 = Pattern. compile ("//. + "); 25 Matcher matcher1 = pattern1.matcher (sb); 26 sb = matcher1.replaceAll (" "); // remove a single line comment 27 28 Pattern pattern2 = Pattern. compile ("/\\*. *? \ */", Pattern. DOTALL); 29 // Pattern. the DoTALL field indicates that it can match any character, including the line terminator 30 // System. out. println (pattern2.toString (); 31 Matcher matcher2 = pattern2.matcher (sb); 32 sb = matcher2.replaceAll (""); // remove multi-line comments 33 34 // You can also remove the line break Let's we try it.35 Pattern pattern3 = Pattern. compile ("\ n"); 36 Matcher matcher3 = pattern3.matcher (sb); 37 sb = matcher3.replaceAll (""); // remove the linefeed 38 39 Pattern pattern4 = Pattern. compile ("\\ T "); 40 Matcher matcher4 = pattern4.matcher (sb); 41 sb = matcher4.replaceAll (" "); // remove the tab 42 43 Pattern pattern5 = Pattern. compile ("\". + \ ""); 44 Matcher matcher5 = pattern5.matcher (sb); 45 sb = matcher5.replaceAll (""); // output string 46 47 // System. out. println (sb); 48 addKeywords (); // Add the keyword to Map. No problem. la 49 String [] strs = sb. split ("[.,; :!? () {}] "); 50 // traverse each word if it is a keyword + + 51 for (int I = 0; I <strs. length; I ++) {52 if (map. containsKey (strs [I]) {53 int value = map. get (strs [I]) + 1; 54 map. put (strs [I], value ); 55} 56} 57 58 // traverse the map. If the value is greater than 0, output59 // Of course, the 60 Set <Map. entry <String, Integer> set = map. entrySet (); 61 // then use foreach to traverse 62 for (Map. entry <String, Integer> entry: set) {63 if (entry. getValue ()> 0) {64 System. out. println (entr Y. getKey () + ": \ t" + entry. getValue (); 65} 66} 67} 68 69 public static void addKeywords () {70 String [] keywordString = {"abstract", "assert", "boolean ", 71 "break", "byte", "case", "catch", "char", "class", 72 "const", "continue", "default ", "do", "double", 73 "else", "enum", "extends", "for", "final", "finally", 74 "float ", "goto", "if", "implements", "import", "instanceof", 75 "int", "interface", "lon G "," native "," new "," package ", 76" private "," protected "," public "," return "," short ", 77 "static", "strictfp", "super", "switch", "synchronized", 78 "this", "throw", "throws", "transient ", "try", 79 "void", "volatile", "while", "true", "false", "null"}; 80 for (int I = 0; I <keywordString. length; I ++) {81 map. put (keywordString [I], 0); 82} 83} 84} 85/** 86 * Summary: this still has bugs such: 87 * for (int88 * words cannot be matched.. 89 * However, I think I should have completed the exercise requirements in the book. 90 */

However, I seem to have fixed the bug.

Sb stands for StringBuilder.

 

Add the source code (input.txt file) for my test)

1 import java. io. file; 2 import java. util. optional; 3 4 public class Main {5 static int totalLines = 0; 6 public static void main (String [] args) throws Exception {7 File file = new File ("E: \ mycode "); 8 File [] files = file. listFiles (); // put the file in mycode into files 9 fileReader (files); 10 System. out. println (totalLines); 11} 12 13 public static void fileReader (File [] files) throws Exception {14 for (int I = 0; I <files. length; I ++) {15 if (files [I]. isFile () {16 if (files [I]. toString (). matches (". * java ") {17 totalLines + = lines (files [I]); 18} 19} 20 else if (files [I]. isDirectory () 21 fileReader (files [I]. listFiles (); 22} 23} 24 25 public static int lines (File file) throws Exception {26 rows = new rows (file); 27 int lines = 0; 28 while (response. hasNext () 29 {30 seconds. nextLine (); 31 lines ++; 32} 33 bytes. close (); 34 return lines; 35} 36}

This test code is a code that calculates the amount of my code when I first learned File.

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.