找回密码
 注册

QQ登录

只需一步,快速开始

查看: 627|回复: 3

Meta-characters

[复制链接]
发表于 2013-10-28 12:30:29 | 显示全部楼层 |阅读模式
        The  power  of  regular  expressions comes from the   ability to include alternatives and repetitions in the   pattern.  These  are encoded in the pattern by the use of    meta-characters, which do not stand for  themselves  but  instead   are interpreted in some special way.  
  
   There are two different sets of meta-characters: those  that   are  recognized anywhere in the pattern except within square   brackets, and those that are recognized in square brackets.   Outside square brackets, the meta-characters are as follows:
\     general escape character with several uses
^     assert start of subject (or line, in multiline mode)
$     assert end of subject or before a terminating newline (or end of line, in multiline mode)
.     match any character except newline (by default)
[     start character class definition
]     end character class definition
|     start of alternative branch
(     start subpattern
)     end subpattern
?     extends the meaning of (, also 0 or 1 quantifier, also makes greedy quantifiers lazy (see repetition)
*     0 or more quantifier
+     1 or more quantifier
{     start min/max quantifier
}     end min/max quantifier

Part of a pattern that is in square brackets is called a "character class". In a character class the only meta-characters are:
\     general escape character
^     negate the class, but only if the first character
-     indicates character range

The following sections describe the use of each of the meta-characters.


 楼主| 发表于 2013-10-30 09:23:07 | 显示全部楼层

Escape sequences

   The backslash character has several uses. Firstly, if it  is   followed by a non-alphanumeric character, it takes away any   special  meaning that character may have. This use of   backslash as an escape character applies both inside and   outside character classes.  
  
   For example, if you want to match a "*" character, you write   "\*" in the pattern. This applies whether or not the   following character would otherwise be interpreted as a   meta-character, so it is always safe to precede a non-alphanumeric   with "\" to specify that it stands for itself.  In   particular, if you want to match a backslash, you write "\\".
Note:

Single and double quoted PHP strings have special meaning of backslash. Thus if \ has to be matched with a regular expression \\, then "\\\\" or '\\\\' must be used in PHP code.

   If a pattern is compiled with the   PCRE_EXTENDED option,   whitespace in the pattern (other than in a character class) and   characters between a "#" outside a character class and the next newline   character are ignored. An escaping backslash can be used to include a   whitespace or "#" character as part of the pattern.  
  
   A second use of backslash provides a way of encoding   non-printing characters in patterns in a visible manner. There   is no restriction on the appearance of non-printing  characters,   apart from the binary zero that terminates a pattern,   but when a pattern is being prepared by text editing, it is   usually  easier to use one of the following escape sequences   than the binary character it represents:

  1. \a alarm, that is, the BEL character (hex 07)
  2. \cx "control-x", where x is any character
  3. \e escape (hex 1B)
  4. \f formfeed (hex 0C)
  5. \n newline (hex 0A)
  6. \p{xx} a character with the xx property, see unicode properties for more info
  7. \P{xx} a character without the xx property, see unicode properties for more info
  8. \r carriage return (hex 0D)
  9. \t tab (hex 09)
  10. \xhh character with hex code hh
  11. \ddd character with octal code ddd, or backreference
复制代码

   The precise effect of "\cx" is as follows:    if "x" is a lower case  letter, it is converted   to upper case. Then bit 6 of the character (hex 40) is inverted.    Thus "\cz" becomes  hex 1A, but   "\c{" becomes hex 3B, while "\c;"   becomes hex 7B.  
  
   After "\x", up to two hexadecimal digits are   read (letters can be in upper or lower case).   In UTF-8 mode, "\x{...}" is   allowed, where the contents of the braces is a string of hexadecimal   digits. It is interpreted as a UTF-8 character whose code number is the   given hexadecimal number. The original hexadecimal escape sequence,   \xhh, matches a two-byte UTF-8 character if the value   is greater than 127.  
  
   After "\0" up to two further octal digits are read.   In  both cases,  if  there are fewer than two digits, just those that   are present are used. Thus the sequence "\0\x\07"    specifies two binary zeros followed by a BEL character. Make sure you   supply two digits after the initial zero if the character   that follows is itself an octal digit.  
  
   The handling of a backslash followed by a digit other than 0   is complicated. Outside a character class, PCRE reads it   and any following digits as a decimal number. If the  number   is  less  than  10, or if there have been at least that many   previous capturing left parentheses in the  expression,  the   entire  sequence is taken as a back reference. A description   of how this works is given later, following  the  discussion   of parenthesized subpatterns.  
  
   Inside a character  class,  or  if  the  decimal  number  is   greater than 9 and there have not been that many capturing   subpatterns, PCRE re-reads up to three octal digits following    the backslash, and generates a single byte from the   least significant 8 bits of the value. Any subsequent digits   stand for themselves.  For example:

  1. \040 is another way of writing a space
  2. \40 is the same, provided there are fewer than 40 previous capturing subpatterns
  3. \7 is always a back reference
  4. \11 might be a back reference, or another way of writing a tab
  5. \011 is always a tab
  6. \0113 is a tab followed by the character "3"
  7. \113 is the character with octal code 113 (since there can be no more than 99 back references)
  8. \377 is a byte consisting entirely of 1 bits
  9. \81 is either a back reference, or a binary zero followed by the two characters "8" and "1"
复制代码

   Note that octal values of 100 or greater must not be   introduced by a leading zero, because no more than three octal   digits are ever read.  
  
   All the sequences that define a single byte value can  be   used both inside and outside character classes. In addition,   inside a character class, the sequence "\b"   is interpreted as the backspace character (hex 08). Outside a character   class it has a different meaning (see below).  
  
   The third use of backslash is for specifying generic   character types:

  1. \d any decimal digit
  2. \D any character that is not a decimal digit
  3. \h any horizontal whitespace character (since PHP 5.2.4)
  4. \H any character that is not a horizontal whitespace character (since PHP 5.2.4)
  5. \s any whitespace character
  6. \S any character that is not a whitespace character
  7. \v any vertical whitespace character (since PHP 5.2.4)
  8. \V any character that is not a vertical whitespace character (since PHP 5.2.4)
  9. \w any "word" character
  10. \W any "non-word" character
复制代码

   Each pair of escape sequences partitions the complete set of   characters into two disjoint sets. Any given character   matches one, and only one, of each pair.  
  
   A "word" character is any letter or digit or the underscore   character,  that  is,  any  character which can be part of a   Perl "word". The definition of letters and digits is     controlled by PCRE's character tables, and may vary if locale-specific   matching is taking place. For example, in the "fr" (French) locale, some   character codes greater than 128 are used for accented letters,   and these are matched by \w.  
  
   These character type sequences can appear both inside and   outside  character classes. They each match one character of   the appropriate type. If the current matching  point is at   the end of the subject string, all of them fail, since there   is no character to match.  
  
   The fourth use of backslash is  for  certain  simple   assertions. An assertion specifies a condition that has to be met   at a particular point in  a match, without consuming any   characters from the subject string. The use of subpatterns   for more complicated assertions is described below. The   backslashed assertions are

  1. \b word boundary
  2. \B not a word boundary
  3. \A start of subject (independent of multiline mode)
  4. \Z end of subject or newline at end (independent of multiline mode)
  5. \z end of subject (independent of multiline mode)
  6. \G first matching position in subject
复制代码

   These assertions may not appear in  character  classes  (but   note that "\b" has a different meaning, namely the backspace   character, inside a character class).  
  
   A word boundary is a position in the subject string where   the current character and the previous character do not both   match \w or \W (i.e. one matches    \w and  the  other  matches   \W), or the start or end of the string if the first   or last character matches \w, respectively.  
  
   The \A, \Z, and   \z assertions differ  from  the  traditional   circumflex  and  dollar  (described below) in that they only   ever match at the very start and end of the subject  string,   whatever  options  are  set.  They  are  not affected by the   PCRE_MULTILINE or   PCRE_DOLLAR_ENDONLY   options. The  difference  between \Z and   \z  is that \Z matches before a   newline that is the last character of the string as well as at the end of   the string, whereas \z matches only at the end.  
  
   The \G assertion is true only when the current   matching position is at the start point of the match, as specified by   the offset argument of    preg_match(). It differs from \A   when the value of offset is non-zero.  
   
   \Q and \E can be used to ignore   regexp metacharacters in the pattern. For example:   \w+\Q.$.\E$ will match one or more word characters,   followed by literals .$. and anchored at the end of   the string.  
   
   \K can be used to reset the match start since   PHP 5.2.4. For example, the pattern foo\Kbar matches   "foobar", but reports that it has matched "bar". The use of   \K does not interfere with the setting of captured   substrings. For example, when the pattern (foo)\Kbar   matches "foobar", the first substring is still set to "foo".  







 楼主| 发表于 2013-10-30 09:25:48 | 显示全部楼层

Pattern Modifiers

本帖最后由 immvan 于 2013-10-30 09:27 编辑

The current possible PCRE modifiers are listed below.  The names   in parentheses refer to internal PCRE names for these modifiers.   Spaces and newlines are ignored in modifiers, other characters cause error.
i (PCRE_CASELESS)
    If this modifier is set, letters in the pattern match both upper and lower case letters.
m (PCRE_MULTILINE)
    By default, PCRE treats the subject string as consisting of a single "line" of characters (even if it actually contains several newlines). The "start of line" metacharacter (^) matches only at the start of the string, while the "end of line" metacharacter ($) matches only at the end of the string, or before a terminating newline (unless D modifier is set). This is the same as Perl. When this modifier is set, the "start of line" and "end of line" constructs match immediately following or immediately before any newline in the subject string, respectively, as well as at the very start and end. This is equivalent to Perl's /m modifier. If there are no "\n" characters in a subject string, or no occurrences of ^ or $ in a pattern, setting this modifier has no effect.
s (PCRE_DOTALL)
    If this modifier is set, a dot metacharacter in the pattern matches all characters, including newlines. Without it, newlines are excluded. This modifier is equivalent to Perl's /s modifier. A negative class such as [^a] always matches a newline character, independent of the setting of this modifier.
x (PCRE_EXTENDED)
    If this modifier is set, whitespace data characters in the pattern are totally ignored except when escaped or inside a character class, and characters between an unescaped # outside a character class and the next newline character, inclusive, are also ignored. This is equivalent to Perl's /x modifier, and makes it possible to include comments inside complicated patterns. Note, however, that this applies only to data characters. Whitespace characters may never appear within special character sequences in a pattern, for example within the sequence (?( which introduces a conditional subpattern.
e (PREG_REPLACE_EVAL)
    If this modifier is set, preg_replace() does normal substitution of backreferences in the replacement string, evaluates it as PHP code, and uses the result for replacing the search string. Single quotes, double quotes, backslashes (\) and NULL chars will be escaped by backslashes in substituted backreferences.

    Only preg_replace() uses this modifier; it is ignored by other PCRE functions.
A (PCRE_ANCHORED)
    If this modifier is set, the pattern is forced to be "anchored", that is, it is constrained to match only at the start of the string which is being searched (the "subject string"). This effect can also be achieved by appropriate constructs in the pattern itself, which is the only way to do it in Perl.
D (PCRE_DOLLAR_ENDONLY)
    If this modifier is set, a dollar metacharacter in the pattern matches only at the end of the subject string. Without this modifier, a dollar also matches immediately before the final character if it is a newline (but not before any other newlines). This modifier is ignored if m modifier is set. There is no equivalent to this modifier in Perl.
S
    When a pattern is going to be used several times, it is worth spending more time analyzing it in order to speed up the time taken for matching. If this modifier is set, then this extra analysis is performed. At present, studying a pattern is useful only for non-anchored patterns that do not have a single fixed starting character.
U (PCRE_UNGREEDY)
    This modifier inverts the "greediness" of the quantifiers so that they are not greedy by default, but become greedy if followed by ?. It is not compatible with Perl. It can also be set by a (?U) modifier setting within the pattern or by a question mark behind a quantifier (e.g. .*?).
X (PCRE_EXTRA)
    This modifier turns on additional functionality of PCRE that is incompatible with Perl. Any backslash in a pattern that is followed by a letter that has no special meaning causes an error, thus reserving these combinations for future expansion. By default, as in Perl, a backslash followed by a letter with no special meaning is treated as a literal. There are at present no other features controlled by this modifier.
J (PCRE_INFO_JCHANGED)
    The (?J) internal option setting changes the local PCRE_DUPNAMES option. Allow duplicate names for subpatterns.
u (PCRE8)
    This modifier turns on additional functionality of PCRE that is incompatible with Perl. Pattern strings are treated as UTF-8. This modifier is available from PHP 4.1.0 or greater on Unix and from PHP 4.2.3 on win32. UTF-8 validity of the pattern is checked since PHP 4.3.5.

发表于 2013-11-20 15:00:39 | 显示全部楼层
本帖最后由 恋恋风尘 于 2013-11-21 07:21 编辑

the symbols '*', '+', and '?', which denote the number of times a character or a sequence of
characters may occur. What they mean is: "zero or more", "one or more", and "zero or one." Here are some examples:

"ab*": matches a string that has an a followed by zero or more b's ("a", "ab", "abbb", etc.);
"ab+": same, but there's at least one b ("ab", "abbb", etc.);
"ab?": there might be a b or not;
"a?b+$": a possible a followed by one or more b's ending a string.
You can also use bounds, which come inside braces and indicate ranges in the number of occurences:

"ab{2}": matches a string that has an a followed by exactly two b's ("abb");
"ab{2,}": there are at least two b's ("abb", "abbbb", etc.);
"ab{3,5}": from three to five b's ("abbb", "abbbb", or "abbbbb").
Note that you must always specify the first number of a range (i.e, "{0,2}", not "{,2}"). Also, as you might
have noticed, the symbols '*', '+', and '?' have the same effect as using the bounds "{0,}", "{1,}", and "{0,1}",
respectively.

More examples seehere.
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|BC Morning Website ( Best Deal Inc. 001 )

GMT-8, 2026-5-27 21:57 , Processed in 0.019304 second(s), 17 queries .

Supported by Weloment Group X3.5

© 2008-2026 Best Deal Online

快速回复 返回顶部 返回列表