About Regular Expressions |
A regular expression is a way of specifying a search string based on common characteristics shared by each string in the set. Regular expressions can range from simple to complex, and the syntax is extensive. There are many different varieties of regular expressions, including grep, Perl, Tcl, Python, PHP, and awk. The IDE uses the Java package java.util.regex to perform the search. See java.util.regex.Pattern for a complete list of regular expression constructs that search a single line at at time in files. This topic presents some of the more common elements of the regular expressions used by the IDE.
Regular expressions use a number of special characters that specify a pattern to be matched. For example, the search string "java." matches "java" followed by a single character. This regular expression would match both "java." and "javax" in a source file.
The metacharacters supported by regular expressions are ([{\^$|)?*+. The following table shows how to use some of the metacharacters:
| Expression | Description |
|---|---|
| ^ | The beginning of a line |
| $ | The end of a line |
| [abc] | a, b, or c Example: |
| [^abc] | Any character except a, b, or c
Example: |
| [a-zA-Z] | a through z or A through Z, inclusive |
| [a-d[m-p]] | a through d, or m through p: [a-dm-p] |
| [a-z&&[def]] | d, e, or f |
| . | Any character |
| \d | A digit: [0-9] |
| \D | A non-digit: [^0-9] |
| \s | A white space character: |
| \S | A non-white space character: [^\s] |
| \w | A word character: [a-zA-Z_0-9] |
| \W | A non-word character: [^\w] |
| Quantifier | Description |
|---|---|
| X? | Match X once or not at all. |
| X* | Match X zero or more times. |
| X+ | Match X one or more times. |
| X{n} | Match X exactly n times. |
| X{n,} | Match X at least n times. |
| X{n,m} | Match X at least n but not more than m times. |
There are two ways to force a metacharacter to be treated as an ordinary character:
| Legal Notices. Copyright © 2006 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms. |