Home IT Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Regular expressions in Perl     - Linux common network tools: batch scanning of hosting services netcat (Linux)

- Monitoring network traffic with Iptraf in Linux environment (Linux)

- Shell array: Define Shell array, the array length (Programming)

- Java uses JDBC connect database (Programming)

- The Linux firewall is configured to use proxy (Linux)

- Teach you how to synchronize Microsoft OneDrive in Linux (Linux)

- How do I delete a NEEDS RECOVERY rollback state of undo tablespace (Database)

- How to achieve the MySQL Oracle table associated with the two operations update (Database)

- Qt signals and slots mechanism (Programming)

- Linux, security encryption to transfer files between machines (Linux)

- Node.js developers must know four JavaScript concepts (Programming)

- Ubuntu terminal command - see the port occupied and off (Linux)

- The correct method to modify the maximum number of concurrent connections Tomcat 7 (Server)

- How to fix fatal error: security / pam_modules.h: No such file or directory (Linux)

- CentOS Linux Optimization and real production environment (Linux)

- Spring WebSocket Comments (Programming)

- Struts2 form of non-use component tags (Programming)

- Btrfs file system repair techniques (Linux)

- Hibernate in profile (Database)

- Why did not Oracle privileges can also log in with sysdba (Database)

 
         
  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:      
 
- Spacewalk Linux system configuration and installation (Linux)
- RHEL 6.5 KVM analytical use (Server)
- Ganglia Python plug-in the process of writing notes (Linux)
- Ubuntu and derivative users to install the system launcher SimDock 1.3 (Linux)
- Oracle table compression Technology Introduction (Database)
- MySQL stored procedures and triggers (Database)
- How to generate Linux, random password encryption or decryption (Linux)
- Ubuntu to install systems Indicator Sticky Notes 0.4.4 (Linux)
- Fedora 8 configured Heartbeat (Linux)
- Squid proxy server (Server)
- RedHat Performance Tuning (Server)
- LVM mirrored logical volume to achieve (Linux)
- IPTABLES configuration steps under Linux (Linux)
- RedHat Redis Linux installation (Database)
- Linux System Getting Started Learning: compile and install ixgbe driver in Ubuntu or Debian (Linux)
- Ubuntu 14.04 compile RT288x_SDK / toolchain / squashfs3.2-r2 Error (Linux)
- Linux system security settings after installation (Linux)
- To install Jetty server configuration in detail (Server)
- Search Linux commands and files - which, whereis, locate, find (Linux)
- Find details block device with Linux blkid command (Linux)
     
           
     
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.