Regular Expressions

In Search and Replace operations, complex pattern matching is provided through regular expressions.
Regular expressions are a combination of regular characters with special characters that describe various types of matching.
These special characters are called meta-characters.

Character Meaning
. Match any single character
* Match zero or more of any character
^ Anchor at beginning of line (^dog only matches dog at the beginning of a line)
$ Anchor at end of line (dog$ only matches dog at the end of a line)
\ Take the following special character literally (\. matches a period)
[...] Match a single position to anything between the brackets. For example "d[io]g" matches to both "dig" and "dog"
[^...] Match a single position to anything that is not between the brackets. For example "d[^io]g" would not match "dig" or "dog" but would match "dug".
\(...\) In a regular expression, these marks are used to save parts of the matched text for use in a substitute operation.
Up to nine of these pairs can appear in a regular expression, and in a substitution pattern these can be referenced as \1 through \9.
\<...\> Use these delimiters to indicate that whatever is in between them must exist as a word in order to be matched. This means that it must have either white space or punctuation on either side of it.

An Emacs Regular Expression Tip

Refer to http://www.delorie.com/gnu/docs/emacs/emacs_85.html

M-x replace-regexp example
FROM: ((String\[\]) reqMap\.get(\(.*\)))\[0\]
TO: request.getParameter(\1)

NOTE that
\( \) has special meaning for matched words

A Java (SDK 1.4) Regular Expression Example

Search http:// and mailto:// url in the some text and replace it with some appropriate links.
import java.util.regex.*;

    final static String EXPR = "http://([0-9a-zA-Z./@~?&=_-]+)|([_0-9a-zA-Z-]+(\\.[_0-9a-zA-Z-]+)*)@([0-9a-zA-Z-]+(\\.[0-9a-zA-Z-]+)*)|<|>|\\|\r|\n"; // http or email

    /**
     * handle body text
     */
    public String handleBodyText(String data) {
	StringBuffer buffer = new StringBuffer(data.length() + 30);
	Matcher matcher = Pattern.compile(EXPR).matcher(data);

	int count = 0;
	while (matcher.find()) {
	    count++;
	    String group = matcher.group();
	    if (group.startsWith("http://")) { // http address
		// $# indicates group tokens and $0 always indicates the whole matched string
		matcher.appendReplacement(buffer, "<a href=\"$0\" target=\"_blank\">$0</A>");
	    } else {
		switch (group.charAt(0)) {
		case '<' :
		    matcher.appendReplacement(buffer, "%26lt;");
		    break;
		case '>' :
		    matcher.appendReplacement(buffer, "%26gt;");
		    break;
		case '\\' :
		    matcher.appendReplacement(buffer, "%5c");
		    break;
		case '\r' :
		    matcher.appendReplacement(buffer, "<br>");
		    break;
		case '\n' :
		    matcher.appendReplacement(buffer, "<br>");
		    break;
		default : // email address
		    matcher.appendReplacement(buffer, "<a href=\"mailto:$0\">$0</A>");
		    break;
		}
	    }

	}

	if (count > 0) {
	    matcher.appendTail(buffer);
	    return buffer.toString();
	}

	return data;
    }

Yoon Kyung Koo <yoonforh at yahoo dot com>
Last modified: Wed Mar 19 00:59:25 +0900 2003