Home IT Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Regular expressions in Perl     - Netapp storage routine inspections and information gathering (Linux)

- MySQL5.7 JSON type using presentation (Database)

- Linux iptables firewall settings (Linux)

- Linux GCC 5.1.0 compiler installation (Linux)

- How to update the Linux kernel to improve system performance (Linux)

- ethtool command Detailed (Linux)

- Oracle to create an external table (Database)

- Recycle Bin function realization in Linux (Linux)

- Related to optimize the use of Btrfs file system on SSD (Linux)

- Linux Fundamentals of the text, data flow processing orders (Linux)

- Effective Java - lazy initialization (Programming)

- Ubuntu 14.04 + xRDP + Xfce implement Windows Remote Desktop Connection (Linux)

- Linux system boot process ARM platforms (Linux)

- MySQL EXPLAIN SQL output description (Database)

- Android Action Compendium (Programming)

- Five useful commands to manage file types and system time in linux (Linux)

- How to improve the performance of Ruby On Rails (Linux)

- ARM runtime environment built from scratch using QEMU emulator (Linux)

- 30 minutes with your Quick Start MySQL Tutorial (Database)

- CentOS 7 update source - the use of domestic sources 163 yum (Linux)

 
         
  Regular expressions in Perl
     
  Add Date : 2018-11-21      
         
       
         
  [Regular expressions of the three forms]

First of all, we should know that Perl programs, regular expressions are three forms of existence, they are:
Matches: m / < regexp> / (or / < regexp> /, omitting m)
Replace: s / < pattern> / < replacement> /
Conversions: tr / < pattern> / < replacemnt> /

These three forms are generally used with = ~ or! ~ (Where = ~ that match, read in the whole statement does,! ~ That does not match the whole sentence read does not), and On the left there is a scalar variable to be processed. Without this and the ~ ~! ~ Operator, the default is to process the contents of the $ _ variable. For example:

$ Str = "I love Perl";
$ Str = ~ m / Perl /; # means "1" if a "Perl" string is found in $ str, or "0" otherwise.
$ Str = ~ s / Perl / BASH /; # means to replace the "Perl" string in the variable $ str with "BASH" and return "1" if the match succeeds or "0" otherwise.
$ Str! ~ Tr / A-Z / a-z /; # means to convert all the upper and lower case letters in the variable $ str, or "0" if the conversion occurs;

Moreover:

Foreach (@array)
{
    S / a / b /;
}}
# Here each cycle will be removed from the array @array an element stored in the $ _ variable, and replace the $ _ processing.
 
While (< FILE>)
{
   Print if (m / error /);
}}
# He will print all the lines in the FILE file that contain the error string.
Perl's regular expression if (), the occurrence of matching or replacement () after the model was Perl interpreter automatically assigned to the system $ 1, $ 2, $ 3 ...

 

Consider the following example:


$ String = "I love perl";
$ String = ~ s / (love) / < $ 1> /; # At this point $ 1 = "love", and the replacement of the result is $ string into "I < love>; perl"
$ String = "i love perl";
$ String = ~ s / (i) (. *) (Perl) / < $ 3> $ 2 < $ 1> /;

# Where $ 1 = "i", $ 2 = "love", $ 3 = "perl", and after the substitution $ string becomes "< perl> love < i>"


Replacement s / < pattern> / < replacement> / can also be added at the end of e or g parameters, their meaning is:

S / < pattern> / < rep> / g

# Replace all the < pattern> patterns in the string to be replaced with the < rep> string, not just the first occurrence of the pattern.


S / < pattern> / < replacement> / e

# Means that the < replacement> part is treated as an operator, and this parameter is not used much.


For example, the following example:
$ String = "i: love: perl";
$ String = ~ s /: / * /; # this time $ string = "i * love: perl";
$ String = "i: love: perl";
$ String = ~ s /: / * / g; # this time $ string = "i * love * perl";
$ String = ~ tr / * / /; # at this time $ string = "i love perl";
$ String = "www22cgi44";
$ String = ~ s / (\ d +) / $ 1 * 2 / e;

# (/ D +) represents one or more numeric characters in $ string, and these numeric characters perform the operation of * 2

# So the last $ string becomes "www44cgi44".


Here's a complete example:

#! / Usr / bin / perl -w
 
Print "Please enter a string! \ N";
 
My $ string = < STDIN>; # < STIDN>; for standard input, the user will enter a string
Chomp ($ string); # Remove the last character of the string $ \ string
 
If ($ string = ~ / perl /)
{
    Print "There is a string perl in the input string! \ N";
}}
If the input string contains perl this string, it will display the following prompt.


[Regular Patterns in Regular Expressions]

Here are some common patterns in regular expressions.

/ Pattern / results
Matches all characters except newline characters
X? Matches 0 or once x strings
X * Matches zero or more x-character strings
X + Matches one or more x-character strings
. * Matches any character at zero or one time
. + Matches any character one or more times
{M} Matches exactly the m specified strings
{M, n} Matches more than m specified n character strings
{M,} Matches more than m specified strings
[] Matches characters within []
[^] Matches characters that do not match []
[0-9] Matches all numeric characters
[A-z] matches all lowercase alphabetic characters
[^ 0-9] Matches all non-numeric characters
[^ A-z] Matches all non-lowercase alphabetic characters
^ Match the character at the beginning of the character
$ Matches the character at the end of the character
\ D Matches a numeric character, the same as the [0-9] syntax
\ D + matches multiple numeric strings, just like the [0-9] + syntax
\ D non-digital, the other with \ d
\ D + non-digital, the other with \ d +
\ W English alphabetic or numeric string, the same as [a-zA-Z0-9] syntax
\ W + is the same as the [a-zA-Z0-9] + syntax
\ W Non-alphabetic or numeric string, the same as [^ a-zA-Z0-9]
\ W + is the same as the [^ a-zA-Z0-9] + syntax
\ S spaces, just like the syntax for [\ n \ t \ r \ f]
\ S + is the same as [\ n \ t \ r \ f] +
\ S is not a space, and [^ \ n \ t \ r] the same syntax
\ S + and [^ \ n \ t \ r \ f] + syntax
\ B Matches a string that is surrounded by letters and numbers
\ B Matches a string that is not bounded by an English letter or value
A | b | c Matches a string that matches either the a character or the b character or the c character
Abc Matches a string containing abc


(Pattern) () This symbol will remember the string to find, is a very useful syntax. The string found in the first () becomes $ 1. This variable is either a \ 1 variable. The string found in the second () becomes a $ 2 variable or a \ 2 variable, and so on.

/ Pattern / i i This parameter is ignored in English capitalization, that is, when matching the string, regardless of the English case of the problem.

\ If you want to find a special pattern in the pattern of characters, such as "*", will be in front of the characters with the \ symbol, this will let the special characters

Here are some examples:

Example Description
================================================== ==============
/ Perl / find a string containing perl
/ ^ Perl / Find a string that starts with perl
/ Perl $ / find the end of the string is perl
/ C | g | i / Find a string containing c or g or i
/ Cg {2,4} i / find c followed by 2 to 4 g, followed by the i string
/ Cg {2,} i / find c followed by more than 2 g, followed by the i string
/ Cg {2} i / find c followed by 2 g, followed by the i string
/ Cg * i / find c followed by 0 or more g, followed by the i string, as / cg {0,1} i /
/ Cg + i / find c followed by more than one g, followed by the string i, as / cg {1,} i /
/ Cg? I / find c followed by 0 or 1 g, followed by the i string, as / cg {0,1} i /
/c.i/ find c followed by an arbitrary character, followed by the i string
/c..i/ find c followed by two arbitrary characters, followed by the i string
/ [Cgi] / Find a string that matches any of these three characters
/ [^ Cgi] / Find a string that does not have any of these three characters
/ \ D / Find the characters that match the numbers, you can use / \ d + / to represent one or more digits of the string
/ \ D / Find characters that are not numbers, you can use / \ D + / to represent one or more non-numeric strings
/ \ * / Find the match * this character, because * in the regular expression has its special meaning, \ to escape
/ Abc / i Finds abc-conforming strings without regard to the case of these strings



[Regular expression of the eight principles]

If Unix has been used sed, awk, grep these orders, I believe that the regular expression for the Perl language will not feel strange. Perl language because of this function, so the handling of the string is very strong. In the Perl language program, you can often see the use of regular expressions, in CGI programming is no exception.

Regular expressions are the daunting task of learning Perl, but once you have the syntax, you can have almost unlimited pattern matching capabilities, and most of Perl's work is mastering regular expressions. Here are some regular expressions to introduce the use of the eight principles.

Regular expressions in the fight against data can form a huge alliance - this is often a war. We must remember the following eight principles:


 

Rule 1: There are three different forms of regular expressions (matching (m / /), substitution (s / / / eg), and transformation (tr / / /)).
Rule 2: The regular expression only matches the scalar ($ scalar = ~ m / a /; works; @array = ~ m / a / will treat @array as a scalar and thus may not succeed).
Rule 3: The regular expression matches the earliest possible match for a given pattern. By default, only regular expressions are matched or replaced ($ a = 'string string2'; $ a = ~ s / string / /; resulting in $ a = 'string 2').
Rule 4: Regular expressions can handle any and all characters that double quotes can handle ($ a = ~ m / $ varb / expands varb into a variable before matching; if $ varb = 'a' $ a = 'as' , $ A = ~ s / $ varb / /; is equivalent to $ a = ~ s / a / / ;, and the result is $ a = "s").
Rule 5: There are two cases in regular expression evaluation: $ a = ~ m / pattern / indicates whether there is a substring pattern in $ a, $ a = ~ s / (word1 ) (Word2) / $ 2 $ 1 / then "swap" the two words.
Principle 6: The core competence of regular expressions lies in the wildcard and multiple match operators and how they operate. $ A = ~ m / \ w + / matches one or more word characters; $ a = ~ m / \ d / "matches zero or more digits.
Principle 7: If you want to match more than one character set, Perl uses "|" to increase flexibility. If the input m / (cat | dog) / is equivalent to "matching string cat or dog.
Principle 8: Perl uses the (? ..) syntax to provide extended functionality to regular expressions.
     
         
       
         
  More:      
 
- Those things packaged using Gradle to Android (Programming)
- Nmcli based network management command-line tool (Linux)
- To install Google Chrome browser under Ubuntu 14.04 LTS (Linux)
- Linux find and xargs (Linux)
- Thrift 0.9.3 compiler installation under Ubuntu (Linux)
- How Vim playing a mature IDE (Linux)
- Ubuntu Linux use ufw or iptables firewall configuration (Linux)
- To create a problem by the statement date comparison of the difference between MySQL and Oracle (Database)
- How to handle special characters in JSON (Programming)
- Android float ball and boot from the start (Programming)
- ActiveMQ-based shared file system HA solutions (Server)
- Linux Man Page Installation (Linux)
- Install Redis 2.6 5.5 32 position CentOS error resolved (Linux)
- The next key to install Linux bash script PowerShell (Linux)
- PostgreSQL Source Customization: Online global read only (Database)
- Upload the project to GitHub, synchronous remote repository Github (Linux)
- How to use Git to upload code to GitHub project (Linux)
- Teamviewer not start in Linux (Linux)
- Linux program analysis tool: ldd and nm (Linux)
- Linux reserves the rest of the file to delete several (Linux)
     
           
     
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.