Java Reference
In-Depth Information
ȗ
Do you want the search to go from the point where the cursor is in the file
through to the rest of the file, or should it search the currently selected
text? Restricting replacement to a portion of the file can be very useful,
but in this example you would want to move the cursor to the top of the
file and then replace until the end of the file.
Not every editor has all these options. You should investigate what your editor
offers.
P RODUCTIVITY H INT 8.2: Regular Expressions
Regular expressions describe character patterns. For example, numbers have a
simple form. They contain one or more digits. The regular expression describing
numbers is [0-9]+ . The set [0-9] denotes any digit between 0 and 9 , and the
+ means Ȓone or moreȓ.
What good is it? Several utility programs use regular expressions to locate
matching text. Also, the search commands of some programming editors
understand regular expressions. The most popular program that uses regular
expressions is grep (which stands for Ȓglobal regular expression printȓ). You can
run grep from a command prompt or from inside some compilation
environments. Grep is part of the UNIX operating system, but versions are
available for Windows and MacOS. It needs a regular expression and one or
more files to search. When grep runs, it displays a set of lines that match the
regular expression.
Suppose you want to look for all magic numbers (see Quality Tip 4.1 ) in a file.
The command
grep [0-9]+ Homework.java
362
363
lists all lines in the file Homework.java that contain sequences of digits. That
isn't terribly useful; lines with variable names x1 will be listed. OK, you want
sequences of digits that do not immediately follow letters:
grep [^A-Za-z][0-9]+ Homework.java
Search WWH ::




Custom Search