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. |
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; }