I used the Perl language a few days ago and mainly looked at regular expressions in Perl. In various web languages, regular expressions are very useful when processing strings, so here we will briefly talk about the application of regular expressions in Perl.
Code first
1 #! /Usr/bin/perl-W 2 # Use utf8; 3 # Use encoding "gb2312"; 4 5 open (data, "<test2.txt") or die "read error "; 6 7 open (Out, "> outfile.txt") or die "write error"; 8% array = (); 9 While ($ string = <DATA>) {10 11 if ($ string = ~ /S. *. M/) {12 $ string = ~ S/S. *. M // G; 13 14} 15 chomp ($ string); # Remove the linefeed 16 foreach $ character (split //, $ string) {17 if ($ character = ~ // D $/) 18 {19 $ Array {$ character} ++; 20} # code21} 22} 23 print out "Result: \ n "; 24 @ character = sort {$ Array {$ B} <= >$ Array {$ A} (Keys % array); 25 foreach $ character (@ character) {26 print out "$ character =>$ Array {$ character} \ n"; 27}
To put it simply, read the file content to the string, remove the line break, split by space (spilt), and match the string starting with S and ending with M, then replace it with an empty string (delete), match the string ending with D, save it to the array, and finally output it to the file in descending order. Delete the words starting with S and ending with M, find all words ending with D, and count them.
Regular Expression
(1) Three Forms
Match: M/<Regexp>;/(can also be abbreviated as/<Regexp>;/, skip m)
Replace: S/<pattern >;/ <replacement> ;/
Conversion: TR/<pattern>;/<replacemnt> ;/
These three forms and "= ~" Or "! ~" The matching symbols are used in the preceding code.
(2) Pattern)
. Match All characters except line breaks
X? Match 0 times or once x string
X * matches 0 or multiple times X strings, but the minimum number of possible matches
X + matches the string once or multiple times, but the minimum number of possible matches
. * Match any character 0 or once
. + Match any character once or multiple times
{M} matches a specified string of exactly M.
{M, n} matches a specified string of more than n m
{M,} matches more than m specified strings
[] Match characters in []
[^] Does not match characters in []
[0-9] Match All numeric characters
[A-Z] matches all lowercase letter characters
[^ 0-9] match all non-numeric characters
[^ A-Z] matches all non-lowercase letter characters
^ Match characters starting with a character
$ Match characters at the end of a character
\ D matches the character of a number, which is the same as the [0-9] syntax.
\ D + matches multiple numeric strings, the same as the [0-9] + syntax
\ D is not a number; others are the same as \ D
\ D + non-numeric, others are the same as \ D +
A string of \ W English letters or numbers, the same as the [a-zA-Z0-9] syntax
\ W + the same syntax as [a-zA-Z0-9] +
\ W a string of Non-English letters or numbers, the same as the [^ a-zA-Z0-9] syntax
\ W + and [^ a-zA-Z0-9] + syntax is the same
\ S space, which is the same as the syntax of [\ n \ t \ r \ F]
\ S + is the same as [\ n \ t \ r \ f] +
\ S is not a space, and the syntax is the same as [^ \ n \ t \ r \ F]
The syntax of \ s + is the same as that of [^ \ n \ t \ r \ f] +.
\ B matches strings with English letters and numbers
\ B matches strings that do not contain English letters and numbers.
A | B | C: a string that matches the character, B character, or C character
ABC matches strings containing ABC
Note the use of escape characters in matching. Regular Expressions provide us with a lot of convenience when processing characters. Here we only introduce the simplest use, and we also need to pay attention to the discovery of implicit pattern matching in strings, you will need to learn more in the future.
Regular Expression in Perl