Home | History | Annotate | Download | only in regex
      1 /*
      2  * Copyright (C) 2007 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package java.util.regex;
     18 
     19 import java.io.IOException;
     20 import java.io.ObjectInputStream;
     21 import java.io.Serializable;
     22 
     23 /**
     24  * Patterns are compiled regular expressions. In many cases, convenience methods such as
     25  * {@link String#matches String.matches}, {@link String#replaceAll String.replaceAll} and
     26  * {@link String#split String.split} will be preferable, but if you need to do a lot of work
     27  * with the same regular expression, it may be more efficient to compile it once and reuse it.
     28  * The {@code Pattern} class and its companion, {@link Matcher}, also offer more functionality
     29  * than the small amount exposed by {@code String}.
     30  *
     31  * <pre>
     32  * // String convenience methods:
     33  * boolean sawFailures = s.matches("Failures: \\d+");
     34  * String farewell = s.replaceAll("Hello, (\\S+)", "Goodbye, $1");
     35  * String[] fields = s.split(":");
     36  *
     37  * // Direct use of Pattern:
     38  * Pattern p = Pattern.compile("Hello, (\\S+)");
     39  * Matcher m = p.matcher(inputString);
     40  * while (m.find()) { // Find each match in turn; String can't do this.
     41  *     String name = m.group(1); // Access a submatch group; String can't do this.
     42  * }
     43  * </pre>
     44  *
     45  * <h3>Regular expression syntax</h3>
     46  * <span class="datatable">
     47  * <style type="text/css">
     48  * .datatable td { padding-right: 20px; }
     49  * </style>
     50  *
     51  * <p>Java supports a subset of Perl 5 regular expression syntax. An important gotcha is that Java
     52  * has no regular expression literals, and uses plain old string literals instead. This means that
     53  * you need an extra level of escaping. For example, the regular expression {@code \s+} has to
     54  * be represented as the string {@code "\\s+"}.
     55  *
     56  * <h3>Escape sequences</h3>
     57  * <p><table>
     58  * <tr> <td> \ </td> <td>Quote the following metacharacter (so {@code \.} matches a literal {@code .}).</td> </tr>
     59  * <tr> <td> \Q </td> <td>Quote all following metacharacters until {@code \E}.</td> </tr>
     60  * <tr> <td> \E </td> <td>Stop quoting metacharacters (started by {@code \Q}).</td> </tr>
     61  * <tr> <td> \\ </td> <td>A literal backslash.</td> </tr>
     62  * <tr> <td> &#x005c;u<i>hhhh</i> </td> <td>The Unicode character U+hhhh (in hex).</td> </tr>
     63  * <tr> <td> &#x005c;x<i>hh</i> </td> <td>The Unicode character U+00hh (in hex).</td> </tr>
     64  * <tr> <td> \c<i>x</i> </td> <td>The ASCII control character ^x (so {@code \cH} would be ^H, U+0008).</td> </tr>
     65  *
     66  * <tr> <td> \a </td> <td>The ASCII bell character (U+0007).</td> </tr>
     67  * <tr> <td> \e </td> <td>The ASCII ESC character (U+001b).</td> </tr>
     68  * <tr> <td> \f </td> <td>The ASCII form feed character (U+000c).</td> </tr>
     69  * <tr> <td> \n </td> <td>The ASCII newline character (U+000a).</td> </tr>
     70  * <tr> <td> \r </td> <td>The ASCII carriage return character (U+000d).</td> </tr>
     71  * <tr> <td> \t </td> <td>The ASCII tab character (U+0009).</td> </tr>
     72  * </table>
     73  *
     74  * <h3>Character classes</h3>
     75  * <p>It's possible to construct arbitrary character classes using set operations:
     76  * <table>
     77  * <tr> <td> [abc] </td> <td>Any one of {@code a}, {@code b}, or {@code c}. (Enumeration.)</td> </tr>
     78  * <tr> <td> [a-c] </td> <td>Any one of {@code a}, {@code b}, or {@code c}. (Range.)</td> </tr>
     79  * <tr> <td> [^abc] </td> <td>Any character <i>except</i> {@code a}, {@code b}, or {@code c}. (Negation.)</td> </tr>
     80  * <tr> <td> [[a-f][0-9]] </td> <td>Any character in either range. (Union.)</td> </tr>
     81  * <tr> <td> [[a-z]&&[jkl]] </td> <td>Any character in both ranges. (Intersection.)</td> </tr>
     82  * </table>
     83  * <p>Most of the time, the built-in character classes are more useful:
     84  * <table>
     85  * <tr> <td> \d </td> <td>Any digit character.</td> </tr>
     86  * <tr> <td> \D </td> <td>Any non-digit character.</td> </tr>
     87  * <tr> <td> \s </td> <td>Any whitespace character.</td> </tr>
     88  * <tr> <td> \S </td> <td>Any non-whitespace character.</td> </tr>
     89  * <tr> <td> \w </td> <td>Any word character.</td> </tr>
     90  * <tr> <td> \W </td> <td>Any non-word character.</td> </tr>
     91  * <tr> <td> \p{<i>NAME</i>} </td> <td> Any character in the class with the given <i>NAME</i>. </td> </tr>
     92  * <tr> <td> \P{<i>NAME</i>} </td> <td> Any character <i>not</i> in the named class. </td> </tr>
     93  * </table>
     94  * <p>There are a variety of named classes:
     95  * <ul>
     96  * <li><a href="../../lang/Character.html#unicode_categories">Unicode category names</a>,
     97  * prefixed by {@code Is}. For example {@code \p{IsLu}} for all uppercase letters.
     98  * <li>POSIX class names. These are 'Alnum', 'Alpha', 'ASCII', 'Blank', 'Cntrl', 'Digit',
     99  * 'Graph', 'Lower', 'Print', 'Punct', 'Upper', 'XDigit'.
    100  * <li>Unicode block names, as used by {@link java.lang.Character.UnicodeBlock#forName} prefixed
    101  * by {@code In}. For example {@code \p{InHebrew}} for all characters in the Hebrew block.
    102  * <li>Character method names. These are all non-deprecated methods from {@link java.lang.Character}
    103  * whose name starts with {@code is}, but with the {@code is} replaced by {@code java}.
    104  * For example, {@code \p{javaLowerCase}}.
    105  * </ul>
    106  *
    107  * <h3>Quantifiers</h3>
    108  * <p>Quantifiers match some number of instances of the preceding regular expression.
    109  * <table>
    110  * <tr> <td> * </td> <td>Zero or more.</td> </tr>
    111  * <tr> <td> ? </td> <td>Zero or one.</td> </tr>
    112  * <tr> <td> + </td> <td>One or more.</td> </tr>
    113  * <tr> <td> {<i>n</i>} </td> <td>Exactly <i>n</i>.</td> </tr>
    114  * <tr> <td> {<i>n,</i>} </td> <td>At least <i>n</i>.</td> </tr>
    115  * <tr> <td> {<i>n</i>,<i>m</i>} </td> <td>At least <i>n</i> but not more than <i>m</i>.</td> </tr>
    116  * </table>
    117  * <p>Quantifiers are "greedy" by default, meaning that they will match the longest possible input
    118  * sequence. There are also non-greedy quantifiers that match the shortest possible input sequence.
    119  * They're same as the greedy ones but with a trailing {@code ?}:
    120  * <table>
    121  * <tr> <td> *? </td> <td>Zero or more (non-greedy).</td> </tr>
    122  * <tr> <td> ?? </td> <td>Zero or one (non-greedy).</td> </tr>
    123  * <tr> <td> +? </td> <td>One or more (non-greedy).</td> </tr>
    124  * <tr> <td> {<i>n</i>}? </td> <td>Exactly <i>n</i> (non-greedy).</td> </tr>
    125  * <tr> <td> {<i>n,</i>}? </td> <td>At least <i>n</i> (non-greedy).</td> </tr>
    126  * <tr> <td> {<i>n</i>,<i>m</i>}? </td> <td>At least <i>n</i> but not more than <i>m</i> (non-greedy).</td> </tr>
    127  * </table>
    128  * <p>Quantifiers allow backtracking by default. There are also possessive quantifiers to prevent
    129  * backtracking. They're same as the greedy ones but with a trailing {@code +}:
    130  * <table>
    131  * <tr> <td> *+ </td> <td>Zero or more (possessive).</td> </tr>
    132  * <tr> <td> ?+ </td> <td>Zero or one (possessive).</td> </tr>
    133  * <tr> <td> ++ </td> <td>One or more (possessive).</td> </tr>
    134  * <tr> <td> {<i>n</i>}+ </td> <td>Exactly <i>n</i> (possessive).</td> </tr>
    135  * <tr> <td> {<i>n,</i>}+ </td> <td>At least <i>n</i> (possessive).</td> </tr>
    136  * <tr> <td> {<i>n</i>,<i>m</i>}+ </td> <td>At least <i>n</i> but not more than <i>m</i> (possessive).</td> </tr>
    137  * </table>
    138  *
    139  * <h3>Zero-width assertions</h3>
    140  * <p><table>
    141  * <tr> <td> ^ </td> <td>At beginning of line.</td> </tr>
    142  * <tr> <td> $ </td> <td>At end of line.</td> </tr>
    143  * <tr> <td> \A </td> <td>At beginning of input.</td> </tr>
    144  * <tr> <td> \b </td> <td>At word boundary.</td> </tr>
    145  * <tr> <td> \B </td> <td>At non-word boundary.</td> </tr>
    146  * <tr> <td> \G </td> <td>At end of previous match.</td> </tr>
    147  * <tr> <td> \z </td> <td>At end of input.</td> </tr>
    148  * <tr> <td> \Z </td> <td>At end of input, or before newline at end.</td> </tr>
    149  * </table>
    150  *
    151  * <h3>Look-around assertions</h3>
    152  * <p>Look-around assertions assert that the subpattern does (positive) or doesn't (negative) match
    153  * after (look-ahead) or before (look-behind) the current position, without including the matched
    154  * text in the containing match. The maximum length of possible matches for look-behind patterns
    155  * must not be unbounded.
    156  * <p><table>
    157  * <tr> <td> (?=<i>a</i>) </td> <td>Zero-width positive look-ahead.</td> </tr>
    158  * <tr> <td> (?!<i>a</i>) </td> <td>Zero-width negative look-ahead.</td> </tr>
    159  * <tr> <td> (?&lt;=<i>a</i>) </td> <td>Zero-width positive look-behind.</td> </tr>
    160  * <tr> <td> (?&lt;!<i>a</i>) </td> <td>Zero-width negative look-behind.</td> </tr>
    161  * </table>
    162  *
    163  * <h3>Groups</h3>
    164  *
    165  * <p><table>
    166  * <tr> <td> (<i>a</i>) </td> <td>A capturing group.</td> </tr>
    167  * <tr> <td> (?:<i>a</i>) </td> <td>A non-capturing group.</td> </tr>
    168  * <tr> <td> (?&gt;<i>a</i>) </td> <td>An independent non-capturing group. (The first match of the subgroup is the only match tried.)</td> </tr>
    169  * <tr> <td> \<i>n</i> </td> <td>The text already matched by capturing group <i>n</i>.</td> </tr>
    170  * </table>
    171  * <p>See {@link Matcher#group} for details of how capturing groups are numbered and accessed.
    172  *
    173  * <h3>Operators</h3>
    174  * <p><table>
    175  * <tr> <td> <i>ab</i> </td> <td>Expression <i>a</i> followed by expression <i>b</i>.</td> </tr>
    176  * <tr> <td> <i>a</i>|<i>b</i> </td> <td>Either expression <i>a</i> or expression <i>b</i>.</td> </tr>
    177  * </table>
    178  *
    179  * <a name="flags"><h3>Flags</h3></a>
    180  * <p><table>
    181  * <tr> <td> (?dimsux-dimsux:<i>a</i>) </td> <td>Evaluates the expression <i>a</i> with the given flags enabled/disabled.</td> </tr>
    182  * <tr> <td> (?dimsux-dimsux) </td> <td>Evaluates the rest of the pattern with the given flags enabled/disabled.</td> </tr>
    183  * </table>
    184  *
    185  * <p>The flags are:
    186  * <table>
    187  * <tr><td>{@code i}</td> <td>{@link #CASE_INSENSITIVE}</td> <td>case insensitive matching</td></tr>
    188  * <tr><td>{@code d}</td> <td>{@link #UNIX_LINES}</td>       <td>only accept {@code '\n'} as a line terminator</td></tr>
    189  * <tr><td>{@code m}</td> <td>{@link #MULTILINE}</td>        <td>allow {@code ^} and {@code $} to match beginning/end of any line</td></tr>
    190  * <tr><td>{@code s}</td> <td>{@link #DOTALL}</td>           <td>allow {@code .} to match {@code '\n'} ("s" for "single line")</td></tr>
    191  * <tr><td>{@code u}</td> <td>{@link #UNICODE_CASE}</td>     <td>enable Unicode case folding</td></tr>
    192  * <tr><td>{@code x}</td> <td>{@link #COMMENTS}</td>         <td>allow whitespace and comments</td></tr>
    193  * </table>
    194  * <p>Either set of flags may be empty. For example, {@code (?i-m)} would turn on case-insensitivity
    195  * and turn off multiline mode, {@code (?i)} would just turn on case-insensitivity,
    196  * and {@code (?-m)} would just turn off multiline mode.
    197  * <p>Note that on Android, {@code UNICODE_CASE} is always on: case-insensitive matching will
    198  * always be Unicode-aware.
    199  * <p>There are two other flags not settable via this mechanism: {@link #CANON_EQ} and
    200  * {@link #LITERAL}. Attempts to use {@link #CANON_EQ} on Android will throw an exception.
    201  * </span>
    202  *
    203  * <h3>Implementation notes</h3>
    204  *
    205  * <p>The regular expression implementation used in Android is provided by
    206  * <a href="http://www.icu-project.org">ICU</a>. The notation for the regular
    207  * expressions is mostly a superset of those used in other Java language
    208  * implementations. This means that existing applications will normally work as
    209  * expected, but in rare cases Android may accept a regular expression that is
    210  * not accepted by other implementations.
    211  *
    212  * <p>In some cases, Android will recognize that a regular expression is a simple
    213  * special case that can be handled more efficiently. This is true of both the convenience methods
    214  * in {@code String} and the methods in {@code Pattern}.
    215  *
    216  * @see Matcher
    217  */
    218 public final class Pattern implements Serializable {
    219 
    220     private static final long serialVersionUID = 5073258162644648461L;
    221 
    222     /**
    223      * This constant specifies that a pattern matches Unix line endings ('\n')
    224      * only against the '.', '^', and '$' meta characters. Corresponds to {@code (?d)}.
    225      */
    226     public static final int UNIX_LINES = 0x01;
    227 
    228     /**
    229      * This constant specifies that a {@code Pattern} is matched
    230      * case-insensitively. That is, the patterns "a+" and "A+" would both match
    231      * the string "aAaAaA". See {@link #UNICODE_CASE}. Corresponds to {@code (?i)}.
    232      */
    233     public static final int CASE_INSENSITIVE = 0x02;
    234 
    235     /**
    236      * This constant specifies that a {@code Pattern} may contain whitespace or
    237      * comments. Otherwise comments and whitespace are taken as literal
    238      * characters. Corresponds to {@code (?x)}.
    239      */
    240     public static final int COMMENTS = 0x04;
    241 
    242     /**
    243      * This constant specifies that the meta characters '^' and '$' match only
    244      * the beginning and end of an input line, respectively. Normally, they
    245      * match the beginning and the end of the complete input. Corresponds to {@code (?m)}.
    246      */
    247     public static final int MULTILINE = 0x08;
    248 
    249     /**
    250      * This constant specifies that the whole {@code Pattern} is to be taken
    251      * literally, that is, all meta characters lose their meanings.
    252      */
    253     public static final int LITERAL = 0x10;
    254 
    255     /**
    256      * This constant specifies that the '.' meta character matches arbitrary
    257      * characters, including line endings, which is normally not the case.
    258      * Corresponds to {@code (?s)}.
    259      */
    260     public static final int DOTALL = 0x20;
    261 
    262     /**
    263      * This constant specifies that a {@code Pattern} that uses case-insensitive matching
    264      * will use Unicode case folding. On Android, {@code UNICODE_CASE} is always on:
    265      * case-insensitive matching will always be Unicode-aware. If your code is intended to
    266      * be portable and uses case-insensitive matching on non-ASCII characters, you should
    267      * use this flag. Corresponds to {@code (?u)}.
    268      */
    269     public static final int UNICODE_CASE = 0x40;
    270 
    271     /**
    272      * This constant specifies that a character in a {@code Pattern} and a
    273      * character in the input string only match if they are canonically
    274      * equivalent. It is (currently) not supported in Android.
    275      */
    276     public static final int CANON_EQ = 0x80;
    277 
    278     private final String pattern;
    279     private final int flags;
    280 
    281     transient int address;
    282 
    283     /**
    284      * Returns a {@link Matcher} for this pattern applied to the given {@code input}.
    285      * The {@code Matcher} can be used to match the {@code Pattern} against the
    286      * whole input, find occurrences of the {@code Pattern} in the input, or
    287      * replace parts of the input.
    288      */
    289     public Matcher matcher(CharSequence input) {
    290         return new Matcher(this, input);
    291     }
    292 
    293     /**
    294      * Splits the given {@code input} at occurrences of this pattern.
    295      *
    296      * <p>If this pattern does not occur in the input, the result is an
    297      * array containing the input (converted from a {@code CharSequence} to
    298      * a {@code String}).
    299      *
    300      * <p>Otherwise, the {@code limit} parameter controls the contents of the
    301      * returned array as described below.
    302      *
    303      * @param limit
    304      *            Determines the maximum number of entries in the resulting
    305      *            array, and the treatment of trailing empty strings.
    306      *            <ul>
    307      *            <li>For n &gt; 0, the resulting array contains at most n
    308      *            entries. If this is fewer than the number of matches, the
    309      *            final entry will contain all remaining input.
    310      *            <li>For n &lt; 0, the length of the resulting array is
    311      *            exactly the number of occurrences of the {@code Pattern}
    312      *            plus one for the text after the final separator.
    313      *            All entries are included.
    314      *            <li>For n == 0, the result is as for n &lt; 0, except
    315      *            trailing empty strings will not be returned. (Note that
    316      *            the case where the input is itself an empty string is
    317      *            special, as described above, and the limit parameter does
    318      *            not apply there.)
    319      *            </ul>
    320      */
    321     public String[] split(CharSequence input, int limit) {
    322         return Splitter.split(this, pattern, input.toString(), limit);
    323     }
    324 
    325     /**
    326      * Equivalent to {@code split(input, 0)}.
    327      */
    328     public String[] split(CharSequence input) {
    329         return split(input, 0);
    330     }
    331 
    332     /**
    333      * Returns the regular expression supplied to {@code compile}.
    334      */
    335     public String pattern() {
    336         return pattern;
    337     }
    338 
    339     @Override
    340     public String toString() {
    341         return pattern;
    342     }
    343 
    344     /**
    345      * Returns the flags supplied to {@code compile}.
    346      */
    347     public int flags() {
    348         return flags;
    349     }
    350 
    351     /**
    352      * Returns a compiled form of the given {@code regularExpression}, as modified by the
    353      * given {@code flags}. See the <a href="#flags">flags overview</a> for more on flags.
    354      *
    355      * @throws PatternSyntaxException if the regular expression is syntactically incorrect.
    356      *
    357      * @see #CANON_EQ
    358      * @see #CASE_INSENSITIVE
    359      * @see #COMMENTS
    360      * @see #DOTALL
    361      * @see #LITERAL
    362      * @see #MULTILINE
    363      * @see #UNICODE_CASE
    364      * @see #UNIX_LINES
    365      */
    366     public static Pattern compile(String regularExpression, int flags) throws PatternSyntaxException {
    367         return new Pattern(regularExpression, flags);
    368     }
    369 
    370     /**
    371      * Equivalent to {@code Pattern.compile(pattern, 0)}.
    372      */
    373     public static Pattern compile(String pattern) {
    374         return new Pattern(pattern, 0);
    375     }
    376 
    377     private Pattern(String pattern, int flags) throws PatternSyntaxException {
    378         if ((flags & CANON_EQ) != 0) {
    379             throw new UnsupportedOperationException("CANON_EQ flag not supported");
    380         }
    381         this.pattern = pattern;
    382         this.flags = flags;
    383         compile();
    384     }
    385 
    386     private void compile() throws PatternSyntaxException {
    387         if (pattern == null) {
    388             throw new NullPointerException("pattern == null");
    389         }
    390 
    391         String icuPattern = pattern;
    392         if ((flags & LITERAL) != 0) {
    393             icuPattern = quote(pattern);
    394         }
    395 
    396         // These are the flags natively supported by ICU.
    397         // They even have the same value in native code.
    398         int icuFlags = flags & (CASE_INSENSITIVE | COMMENTS | MULTILINE | DOTALL | UNIX_LINES);
    399 
    400         address = compileImpl(icuPattern, icuFlags);
    401     }
    402 
    403     /**
    404      * Tests whether the given {@code regularExpression} matches the given {@code input}.
    405      * Equivalent to {@code Pattern.compile(regularExpression).matcher(input).matches()}.
    406      * If the same regular expression is to be used for multiple operations, it may be more
    407      * efficient to reuse a compiled {@code Pattern}.
    408      *
    409      * @see Pattern#compile(java.lang.String, int)
    410      * @see Matcher#matches()
    411      */
    412     public static boolean matches(String regularExpression, CharSequence input) {
    413         return new Matcher(new Pattern(regularExpression, 0), input).matches();
    414     }
    415 
    416     /**
    417      * Quotes the given {@code string} using "\Q" and "\E", so that all
    418      * meta-characters lose their special meaning. This method correctly
    419      * escapes embedded instances of "\Q" or "\E". If the entire result
    420      * is to be passed verbatim to {@link #compile}, it's usually clearer
    421      * to use the {@link #LITERAL} flag instead.
    422      */
    423     public static String quote(String string) {
    424         StringBuilder sb = new StringBuilder();
    425         sb.append("\\Q");
    426         int apos = 0;
    427         int k;
    428         while ((k = string.indexOf("\\E", apos)) >= 0) {
    429             sb.append(string.substring(apos, k + 2)).append("\\\\E\\Q");
    430             apos = k + 2;
    431         }
    432         return sb.append(string.substring(apos)).append("\\E").toString();
    433     }
    434 
    435     @Override protected void finalize() throws Throwable {
    436         try {
    437             closeImpl(address);
    438         } finally {
    439             super.finalize();
    440         }
    441     }
    442 
    443     private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException {
    444         s.defaultReadObject();
    445         compile();
    446     }
    447 
    448     private static native void closeImpl(int addr);
    449     private static native int compileImpl(String regex, int flags);
    450 }
    451