/**
* Use under Perl:
*
#Front temperature and Celsius temperature conversion function is more perfect
Print "Please enter the temperature to be converted, for example: 20C/30F\n";
$input = <STDIN>; #Get the input
Chomp($input); #Remove the line break at the end of the text
If($input =~ m/^([-+]?[0-9]+(\.[0-9]*)?) *([CFcf])$/){ # m/^([- +]?[0-9]+)(\.[0-9]*)?([CFcf])$/ is equivalent to:
$inputNum = $1; # m/^([-+]?[0-9]+(\.[0-9]*)?)([CFcf])$/
$type = $2;
If($type eq "F"){
$huashi = ($inputNum * 9 / 5) + 32;
Printf "Fahrenheit temperature: Fahrenheit after %.2f conversion: %.2f\n", $inputNum, $huashi;
}else{
$sheshi = ($inputNum - 32) * 5 / 9 ;
Printf "Celsius temperature: %.2f Celsius temperature after conversion: %.2f\n", $inputNum, $sheshi;
}
}else{
Print "Enter error\"$input\"\n";
}
*
* (?: ) in Perl means that only grouping is not captured
* The regular expression of the above program is changed to m/^([-+]?[0-9]+(?:\.[0-9]*)?) *([CFcf])$/
*
* [ |\t]* is similar to [ *|\t*]: \t indicates blank characters including line breaks, tabs, etc.
* [ |\t]* can match \t\t \t\t \t \t\t\t
* [ *|\t*] can only match: \t\t\t\t or countless spaces
*
* \b is used to match the delimiter of a word
* \s matches all whitespaces including spaces, carriage returns, tabs, newlines.
* m//i : i is a m// modifier that can ignore case.
* :g is the meaning of global matching
* : x loosely arranged expression
* Find and replace:
* $var =~ s/regex/replacement/
* $var =~ s/\bSun\b/Sun/i The role of Sun will be replaced by Sun regardless of the case of the word.
*
* Official letter generation program:
* Leave two or three decimal places for a given number:
* When the third digit after the decimal point is not 0, keep three decimals, otherwise keep two decimals
*/
// $price =~ s/^([0-9]+\.[0-9][0-9][0-9]?)[0-9]*/$1/
// $price =~ s/(\.\d\d[0-9]?)\d*/$1/
/**
* perl -p -i".bak" -e "s/read/ready/g" file
* perl -w mkreply king.in > king.out
* <> operator
* while($line = <>){
* if(line =~ m/^\s*$/){
* last; # Stop the processing inside the while loop, jump out of the loop
* }
* #...handling header information...
* if($line =~ m/^Subject:(.*)/i){
* $subject = $1;
* }
* }
* ...processing other information about the mail...
* Look around: no characters are used during the check match
* Affirmative order look around: (?= ) from left to right
* Affirmative reverse look around: (?<= ) from right to left
* Example: longlong xiao
* (?=xiao) matches the space before xiao.
* Combine look and regular expressions to match positions more accurately.
* For example: longlongxiao and longxiao
* (?=longlognxiao)long matches the two strings above and the second one does not match the result.
* The above expression means matching long, but (?=longlongxiao) limits the content of the string that must be longlongxiao
* A few examples:
* names changed to name's
* 1, s / names / name 's / g
* 2, s/\bnames\b/name's/g
* 3, s/\b(name)(s)\b/$1'$2/g
* 4, s / \ bname (? = s \ b) / name ' / g
* 5, s / (? <= \ bname) (? = s \ b) / ' / g find a position immediately after the name, and before s
*
* A concrete example: add a comma to a long number and a comma every three digits.
* Idea: Start with the right side, insert a comma every three digits, no longer precede the number with a comma.
* Three digits \d\d\d, multiples of three (\d\d\d)+, ending with \b position (?=(\d\d\d)+\b)
* Restrictions are not preceded by a comma, that is, no, plus a comma (?<=\d) in front of 123,456
* Combined: (?<=\d)(?=(\d\d\d)+\b)
* Thinking: (?<=\d)(?(\d\d\d)+\b) and (?=(\d\d\d)+\b)(?<=\d) match results What is the difference? The answer is on the right.
* To improve performance we can change the capture brackets to non-capturing (?<=\d)(?(?:\d\d\d)+\b)
* Use s/(?<=\d)(?=(?:\d\d\d)+\b)/'/g in perl
* Results for 987654321 are: 987, 654, 321
* If you change the regular expression to s/(?<=\d)(?=(?:\d\d\d)+)/'/g, 9,8,7,6,5,4,321 will appear.
* Analysis: first locate a number, then add if there are three digits or a multiple of three digits,
* Two other negative looks (the first two are called a positive look):
* Negative order look around: <?! > (subexpression does not match the text on the right)
* Negative reverse look around: <?<! > (subexpression does not match the text on the left)
* The beginning and ending section breaks of the word:
* Start delimiter: the word is on the right and the word is not on the left (?=\w)(?<!\w)
* End delimiter: the word is on the left and the word is not on the right (?=\w)(?<!\w)
* Then \b (word delimiter) is equivalent to (?=\w)(?<!\w)|(?=\w)(?<!\w)*/
#Word deduplication problem solving
$/ = ".\n"; #Set a special amount of fast mode; read the end of a piece of text as a combination of point and newline
While(<>){ #Read a piece of text saved in the default variable
Next unless s{\b([a-z]+)(?:\s|<[^>]+>)+(\1\b)}
{\e$1\e$2}igx;
s/^(?:[^\e]*\n)+//mg;
s/^/$ARGV:/mg;
Print;
}
Code Analysis:
Java code:
Package RegularExpression;
Import java.io.BufferedReader;
Import java.io.BufferedWriter;
Import java.io.FileNotFoundException;
Import java.io.FileReader;
Import java.io.FileWriter;
Import java.io.IOException;
Import java.util.regex.Pattern;
Import org.slf4j.Logger;
Import org.slf4j.LoggerFactory;
//Words are heavy
Public class WordReduceRepeat {
Private static Logger logger = LoggerFactory.getLogger("RegularExpression");
Private static String fileName = "src/RegularExpression/sun.txt";
Public static void main(String[] args) {
Pattern regex1 = Pattern.compile("\\b([az]+)((?:\\s|\\<[^>]\\>)+)(\\1\\b)",Pattern. CASE_INSENSITIVE);
String replace = "\033$1\033$2\033$3\033";
Pattern regex2 = Pattern.compile("^(?:[^\\e]*\\n)+",Pattern.MULTILINE);
Pattern regex3 = Pattern.compile("^([^\\n]+)",Pattern.MULTILINE);
Try {
BufferedReader be = new BufferedReader(new FileReader(fileName));
String text;
Try{
While((text = getPara(be))!=null){
Text = regex1.matcher(text).replaceAll(replace);
Text = regex2.matcher(text).replaceAll("");
Text = regex3.matcher(text).replaceAll(fileName+":$1");
System.out.println(text);
writeToFile(fileName, text);
}
}catch(IOException e){
Logger.info("file read/write error!"+e);
}finally{
Try {
Be.close();
} catch (IOException e) {
Logger.info ("File close failed!" +e);
}
}
} catch (FileNotFoundException e) {
Logger.info("File not found" +e);
}
}//end main()
/ / Read data from the file, return a string
Public static String getPara(BufferedReader in) throws IOException{
StringBuffer sb = new StringBuffer();
String line;
While(((line = in.readLine()) != null) && ( ((sb.length()) == 0)||((line.length()) != 0)) ){
Sb.append(line+"\n");
}
Return sb.length() == 0 ? null : sb.toString();
}//end getPara()
/ / Save the changes to another file
Public static void writeToFile(String fileName,String content){
String[] path = fileName.split("/");
String newFileName = path[0]+"/"+path[1]+"/"+"new"+path[2];
Try {
BufferedWriter bw = new BufferedWriter(new FileWriter(newFileName));
Try{
Bw.write(content);
}finally{
Bw.flush();
Bw.close();
}
} catch (IOException e) {
Logger.info ("file write error" +e);
}
}//end writeToFile()
}
Beginner Regular Expression 2 (used in Perl)