PHP, Linux, JavaScript regular expressions

Source: Internet
Author: User
Tags character classes modifier alphanumeric characters

Php

The regular expression contains three elements: quantifier, metacharacters, modifier
Quantifiers
Syntax description

    • Matches anything that contains at least one leading string
    • Matches any of 0 or more leading strings
      ? Matches any of 0 or one leading string
      . Match any string
      {x} matches any containing x leading string
      {x, y} matches any leading string containing x to Y
      {x,} matches any containing at least x leading strings
      $ match string end of line
      ^ matches the beginning of a string
      | Match the left or right of a string
      () enclose a character group or define a counter-reference, which can be extracted using \1\2

Metacharacters
Syntax description
[A-z] matches any string containing a A-Z of lowercase letters
[A-z] matches any string containing a A-Z of uppercase letters
[0-9] matches any string containing the number 0-9
[ABC] matches any string containing lowercase letters A, B, c
[ABC] matches any string that does not contain lowercase letters A, B, c
[a-za-z0-9_] matches any string that contains a-za-z0-9 and underscores
\w matches any string that contains a-za-z0-9 and underscores (IBID.)
\w matches any string without underscores and alphanumeric characters
\d matches any numeric character, and [0-9] the same
\d matches any non-numeric character, same as [^0-9]
\s matches any whitespace character
\s matches any non-whitespace character
\b Whether the match reached the word boundary
\b Matches if the word boundary is not reached
\ matches special characters in regular

Modifier
Syntax description
I complete a case-insensitive search
M uses multi-line recognition matching when matching first content or tail content
x ignores whitespace in the regular
A Force A match from the beginning
U disallow greedy match to trace only to the nearest match and end

* preg_filter — 执行一个正则表达式搜索和替换* preg_grep — 返回匹配模式的数组条目* preg_last_error — 返回最后一个PCRE正则执行产生的错误代码* preg_match_all — 执行一个全局正则表达式匹配* preg_match — 执行一个正则表达式匹配* preg_quote — 转义正则表达式字符* preg_replace_callback — 执行一个正则表达式搜索并且使用一个回调进行替换* preg_replace — 执行一个正则表达式的搜索和替换* preg_split — 通过一个正则表达式分隔字符串

$pattern = '/([\d])/([\d]) \ ([\d]*)/';
$string = ' 26/06/2014 ';
Echo preg_replace ($pattern, "$3/$2/$1", $string);

Javascript

 创建方式:      1.            var pattern = new RegExp(‘box‘);           var pattern = new RegExp(‘box‘,‘ig‘);      2.           var pattern = /box/;           var pattern = /box/ig;  

Optional parameters for pattern modifiers
Parameter meaning
I ignore case
G Global Match
M multi-line matching

Methods for REGEXP objects
Method functions
Test matches the pattern in the string, returning True or false
EXEC performs a matching search in a string, returning an array of results
Example 1:var pattern =/box/i;
var str = "This is a box!";
Alert (Pattern.test (str));
Example 2:var pattern =/box/i;
var str = "This is a box!";
Alert (pattern.exec (str));

The String object also provides 4 methods to use a regular expression.

Method meaning
Match (pattern) returns a substring in pattern or null
Replace (pattern, replacement) replaces pattern with replacement
Search (pattern) returns the start position of the pattern in a string
Split (pattern) returns an array of strings split by the specified pattern

Static properties of the RegExp object
Attribute short name meaning
Input $_ The string currently being matched
Lastmatch $& Last matching string
Lastparen $+ the matching substring in the last pair of parentheses
Leftcontext $ ' substring before last match
Multiline $* used to specify whether all expressions are used for multiple rows of Boolean values
Rightcontext $ ' substring after last match

Single character and number
metacharacters/meta symbol matching case
. Match any character except line break
[a-z0-9] matches any character in the character set in parentheses
[a-z0-9] matches any character in a character set that is not in parentheses
\d Matching numbers
\d matches non-numeric, same as [^0-9]
\w matches letters and numbers and
\w matches non-alphabetic and numeric and

Character classes: whitespace characters
metacharacters/meta symbol matching case
Match NULL character
\b Match whitespace characters
\f Matching input characters
\ nthe match line break
\ r matches carriage return character
\ t matches tabs
\s matches white space characters, spaces, tabs, and line breaks
\s matches non-whitespace characters

Character classes: Anchor characters
metacharacters/meta symbol matching case
^ Beginning of match
$ end-of-line matching
\a only at the beginning of the match string
\b Match word boundaries, words are invalid within []
\b Matching non-word boundaries
\g matches the starting position of the current search
\z matches the end of a string or a line ending
\z Match string End only

Character classes: repeating characters
metacharacters/meta symbol matching case
X? Match 0 or 1 x
x* Match 0 or any number of X
x+ matches at least one X
(XYZ) + matches at least one (XYZ)
X{m,n} matches a minimum of M, up to n x

Character classes: Alternative characters
metacharacters/meta symbol matching case
This|where|logo match this or where or any of the logos

Character classes: Record characters
metacharacters/meta symbol matching case
(string) A grouping to use for backward referencing
\1 or match the contents of the first group
\2 or $ match the contents of the second group
\3 or $ A matches the contents of the third group
var pattern =/(\d)/(\d) \ (\d*)/;
var str = ' 26/06/2014 ';
var result = Str.replace (pattern, ' $3/$2/$1 ');
alert (result);

Linux

The character class represents meaning [: alnum:] for English uppercase and lowercase characters and numbers, that is, 0-9,a-z,a-z[:alpha:] for any English-size character, i.e., A-z,a-z[:lower:] For lowercase letters, that is, A-z[:upper:] for uppercase letters, a-z[ :d Igit:] Represents a number, that is, 0-9[:xdigit:] Represents a 16-digit numeric type, so includes the number of the 0-9,a-f,a-f and the character [: Blank:] represents the SPACEBAR and TAB key [: graph:] All but the Space and tab button all other keys [: Space:] Any character that generates whitespace, including SPACEBAR, Tab, CR, etc. [: Cntrl:] represents the control key above the keyboard, including Cr,lf,tab,del, etc. [:p rint:] represents any printable character [:p UNCT:] Represents the punctuation mark, that is "'?!; : # $

Basic Regular expression syntax (re syntax): If a string is represented by a regular expression, any of these characters are called re characters.

Special characters are:
Only the normal regular expression syntax ^ $ is supported.\ [] "'
Supports extended regular expression syntax ^ $.
\ [] "' +? | () basic re character meaning and example ^word
Meaning: the string to be searched (word) at the beginning of the line
Example: Find the line that starts with # at the beginning of #, and the travel number
Grep-n ' ^# ' regular_express.txtword$
Meaning: The string to be found (word) at the end of the line
Example: Print the line at the end of the row, and tie the travel number
Grep-n '!$ ' regular_express.txt.
Meaning: A character that must have an arbitrary character (except for line breaks), which matches the line break in awk
Example: Finding a string can make (Eve) (Eae) (EEE), that is, between E and E must have a character, cannot be (EE)!
Grep-n ' e.e ' regular_express.txt \
Meaning: Escape character, remove special meaning of special symbol, change ordinary character to special character.
Example: Finding the line containing the single quote '
Grep-n \ ' Regular_express.txt
Meaning: Repeat 0 to infinity before one character
Example: Find the string containing (es) (Ess) (ESSS) and so on, note that because
Can be 0, so ES is also compliant with the string to be found. Also, becauseto repeat the symbol "previous re character", so theBe sure to immediately follow an RE character! For example, any character is.
Grep-n ' ESS
' Regular_express.txt[list]
Meaning: Find the character you want to select from the character set's re character (excluding line breaks), and you can include line breaks in awk. Note that the. * in brackets in this case nonspacing into a general character (except []).
Example: Finding the line containing (GL) or (GD), it is very special to note that in [] represents a character to be found, for example, "A[afl]y" means the string to find can be aay,afy, or Aly
Grep-n ' g[ld] ' regular_express.txt[n1-n2]
Meaning: Find the range of characters you want to select from the character set and the re character
Example: Find the line that contains any number. Special attention is paid to the minus sign in the character set []-it has a special meaning, which represents all consecutive characters between two characters (in relation to the encoding order)
Grep-n ' [0-9] ' regular_express.txt[^list]
Meaning: Reverse Selection
Example: The found string can be (Oog) (OOD) but cannot be (oot)
Grep-n ' oo[^t] ' regular_express.txt
N
{N,}
{N,m}
Meaning: A continuous N to M of the previous re character, if {n} is a continuous n of the previous re character, if {n,} is a continuous n more than the previous re character.
In another form in a regular expression that supports extensions, and the n,m must be an integer from 0 to 255:
Note: The syntax for extending regular expressions is essentially
Example: A string of 2 to 3 o existence between G and G
Grep-n ' go{2,3} ' regular_express.txt
4 Extended Regular Expression syntax
To support:
grep needs to be added to-e (or added when using extended symbols)
SED is required to add-r (or add \ When using extended symbols)

The

Awk,perl itself supports extending this expression (that is, if you want to reference in awk (for ordinary characters, [(] use it. Extended re character meaning and example +
meaning: Repeat one or more of the previous re characters
O+ represents more than one O?
Meaning: 0 or one of the previous re characters
O? represents an empty or o|
Meaning: Use or (or) to find a number of strings, the strings on both sides can not add extra space,
abc| def means ABC or def,a (Bc|de) f represents the ABCF or adef
Example: strips blank lines and lines at the beginning of the line
Grep-env ' ^$|^# ' regular_express.txt ()
Meaning: Find the "group" string, [] Extension
Example: Find glad or good these two strings, because G and D are duplicates, so LA and OO can be listed in ()
Grep-en ' g (la|oo) d ' regular_express.txt () + Meaning: Repeat one or more of the previous "group"
{n}
{n,}
{n,m} in the same way as regular regular expressions, except in support of extended regular expressions, which is used in awk, GREP-E, Sed-r.
5, meta-character

Metacharacters: is a Perl-style regular expression, and only a subset of the text-processing tools support it, not all tools.
equivalent to [character set] shorthand. Meta-character meanings and examples \b
Meaning: Word boundaries
Example: \bcool\b matches cool but does not match coolant, special characters cannot be added after the addition of quantifiers \b
Meaning: non-word boundary
Example: cool\b matches coolant but does not match cool, special characters cannot be added after
? + Equal quantifier \d
Meaning: Single digit character
Example: B\db matches business-to-business but does not match bcb\d
Meaning: Single non-numeric characters
Example: B\db matches BCB, but does not match b2b\w
Meaning: Single word characters (letters, numbers, and _)
Example: \w matches 1 or a, but does not match% \w meaning: single non-word character \ n meaning: newline character \s meaning: single whitespace characters, page breaks, tabs, line feeds, carriage returns, and spaces. [\f\t\n\r]\s Meaning: single non-whitespace character \ r meaning: carriage return
6, several useful regular expression item regular expressions match words in regular text
\b[[:alpha:]]+\b
Or
(^| ) ["({[]book[]})"?,.:;! ' S](|$) matches the empty line ^$ matches the blank line containing the space and the blank line ^ space $ matches the entire line ^. $ match one or more spaces space spaces match s preceded by any ABC random combination of String [ABC]s matches formatted dollar amount \$[space 0-9]*. [0-9] [0-9] Match email address [a-za-z0-9.] [Email protected] [A-za-z0-9.] +. [A-za-z] {2,4} matches an HTTP urlhttp://[a-za-z0-9-.] +. [A-za-z] {2,4}

Linux wildcard characters
Shell wildcard characters
Note that the wildcard character here is similar to the regular expression but is parsed based on the Bash interpreter, and the regular expression is parsed by the software of the regular engine (such as awk,grep,sed, etc.), which is completely different.

Wildcard characters

* Represents 0 or more arbitrary characters? A representative must have an arbitrary character [][ABCD], representing a character, or a or B or C or d[-][0-9], representing a number, a [^][^ABC] between 0 and 9, representing a character, and not A, B, c

Example:

[Python] View plaincopy

    1. [[[email protected] ~]# ls test #那个 delegates are accepted after a few characters
    2. [[email protected] ~]# ls tes T? #那个? Represents "must" to be followed by "one" character
    3. [[email protected] ~]# ls test??? #那个??? represents "must be three" characters!
    4. [[email protected] ~]# CP test[1-5]/tmp # will test1, Test2, Test3, test4, test5 if present, copy to/tmp
    5. [[email protected] ~]# CP test[!1-5]/tmp # As long as it is not test1, Test2, Test3, Test4, test5 other than test? Copy to/tmp
    6. [[email protected] ~]# cd/lib/modules/ uname-r /kernel/drivers # The system executes uname-r to find out the output, and adds the result to the directory, To perform the CD function!
    7. = cd/lib/modules/$ (uname-r)/kernel #另外, this quot (') function, can also use $ () to replace Oh!
    8. [[email protected] ~]# CP [A-z] /tmp #表示文件中包含大写字母
    9. [[email protected] ~]# ls-lda/etc/ [] #表示文件中包含数字3或者5.

The regular expression (regular Express) is basically a "notation", which acts as a unit of behavior for string processing. Can only be used in tools that support it (such as VI, grep, awk, sed). The relationship between a regular expression and a shell wildcard is like a relationship between a local variable and a global variable (that is, when a command is encountered that supports regular expressions, the wildcard concept is discarded.) Otherwise, a wildcard character is used).

For more articles, please follow: http://www.ilovehai.com

PHP, Linux, JavaScript regular expressions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.