Requirement: Gets a local file of all the strings in the $[make_package] format and outputs it to another file.
Public Static voidMain (string[] args)throwsException {Loadvar ("Src/cn/don9/templates/action.txt", "Src/cn/don9/templatevar/sysactionvar.txt"); } Public Static voidLoadvar (String infilepath,string Outfilepath)throwsexception{//define set set to achieve de-weight effectset<string> result =NewHashset<string>(); //Defining Regular ExpressionsString regex = "[\\$][\\[]\\w+[\\]]"; Pattern Pattern=pattern.compile (regex); //Read FileFile File =NewFile (Infilepath); BufferedReader BR=NewBufferedReader (Newfilereader (file)); String Line; while((Line=br.readline ())! =NULL){ //string that matches each line in the textMatcher Matcher =Pattern.matcher (line); //found a matching string, stored in set while(Matcher.find ()) {Result.add (Matcher.group ()); } } //Load Output FileFile outfile =NewFile (Outfilepath); BufferedWriter BW=NewBufferedWriter (NewFileWriter (outfile)); for(String var:result) {//Write StringBw.write (var+ "\ r \ n"); } br.close (); Bw.close (); System.out.println ("Finished reading!"); }
Operation Result:
Java regular expression matches the desired string in the text