Example 12-9 prompts to enter a pattern and a string to match. It will output whether the input string matches the pattern. If the input matches the pattern containing the grouping, the program will use parentheses to print the grouping boundary, such as ((11):(59))am
Example 12-9.
1. import .*;
2. import .*;
3.
4. /**
5. This program tests regular expression matching.
6. Enter a pattern and strings to match, or hit Cancel
7. to exit. If the pattern contains groups, the group
8. boundaries are displayed in the match.
9. */
10. public class RegExTest
11. {
12. public static void main(String[] args)
13. {
14. Scanner in = new Scanner();
15. ("Enter pattern: ");
16. String patternString = ();
17.
18. Pattern pattern = null;
19. try
20. {
21. pattern = (patternString);
22. }
23. catch (PatternSyntaxException e)
24. {
25. ("Pattern syntax error");
26. (1);
27. }
28.
29. while (true)
30. {
31. ("Enter string to match: ");
32. String input = ();
33. if (input == null || ("")) return;
34. Matcher matcher = (input);
35. if (())
36. {
37. ("Match");
38. int g = ();
39. if (g > 0)
40. {
41. for (int i = 0; i < (); i++)
42. {
43. for (int j = 1; j <= g; j++)
44. if (i == (j))
45. ('(');
46. ((i));
47. for (int j = 1; j <= g; j++)
48. if (i + 1 == (j))
49. (')');
50. }
51. ();
52. }
53. }
54. else
55. ("No match");
56. }
57. }
58. }
Normally, you don't want to match the entire input to a regular expression, but instead want to find one or more matching substrings in the input. Use the find method of the Matcher class to find the next match. If it returns True, use the start and end methods to find the matching range.
while (())
{
int start = ();
int end = ();
String match = (start, end);
. . .
}
This mechanism is used in Example 12-10. It locates all hypertext references in a web page and prints them. To run the program, provide a URL on the command line, for example
java HrefMatch
Example 12-10.
1. import .*;
2. import .*;
3. import .*;
4.
5. /**
6. This program displays all URLs in a web page by
7. matching a regular expression that describes the
8. <a href=...> HTML tag. Start the program as
9. java HrefMatch URL
10. */
11. public class HrefMatch
12. {
13. public static void main(String[] args)
14. {
15. try
16. {
17. // get URL string from command line or use default
18. String urlString;
19. if ( > 0) urlString = args[0];
20. else urlString = "";
21.
22. // open reader for URL
23. InputStreamReader in = new InputStreamReader(new URL(urlString).openStream());
24.
25. // read contents into string buffer
26. StringBuilder input = new StringBuilder();
27. int ch;
28. while ((ch = ()) != -1) ((char) ch);
29.
30. // search for all occurrences of pattern
31. String patternString = "<a\\s+href\\s*=\\s*(\"[^\"]*\"|[^\\s>])\\s*>";
32. Pattern pattern = (patternString, Pattern.CASE_INSENSITIVE);
33. Matcher matcher = (input);
34.
35. while (())
36. {
37. int start = ();
38. int end = ();
39. String match = (start, end);
40. (match);
41. }
42. }
43. catch (IOException e)
44. {
45. ();
46. }
47. catch (PatternSyntaxException e)
48. {
49. ();
50. }
51. }
52. }
The replaceAll method of the Matcher class replaces the match of all regular expressions that appear with a replacement string. For example, the following command replaces all numeric sequences with #
Pattern pattern = ("[0-9]+");
Matcher matcher = (input);
String output = ("#");
The replacement string can contain grouping references in the pattern: $n is replaced by the nth group. When $ appears in the replacement text, use \$ to include it.
The replaceFirst method only replaces the first occurrence of the pattern.
Finally, the Pattern class has a split method, which is similar to the string tokenizer. It uses regular expression matching as a boundary, separating the input into a string array. For example, the following command separates the input into tokens.
Pattern pattern = ("\\s*\\p{Punct}\\s*");
String[] tokens = (input);
kind
--------------------------------------------------------------------------------
1.4
--------------------------------------------------------------------------------
method
static Pattern compile(String expression)
static Pattern compile(String expression, int flags)
Quick processing for compiling regular expression strings to pattern objects for matching
parameter:
expression regular expression
flags One or more of the following flags CASE_INSENSITIVE, UNICODE_CASE, MULTILINE, UNIX_LINES, DOTALL, and CANON_EQ
Matcher matcher(CharSequence input)
Returns a matcher object that can be used to locate pattern matching in an input
String[] split(CharSequence input)
String[] split(CharSequence input, int limit)
Separate the input string into tokens and specify the form of the delimiter by pattern. Returns an array of tokens. The separator is not part of the token.
parameter:
input is separated into a string that is marked
limit The maximum number of strings generated.
--------------------------------------------------------------------------------
kind
--------------------------------------------------------------------------------
1.4
--------------------------------------------------------------------------------
method
--------------------------------------------------------------------------------
boolean matches()
Returns whether the input matches the pattern
boolean lookingAt()
Return True if the input start matching pattern
boolean find()
boolean find(int start)
Try to find the next match and return True when it finds a match
parameter:
start index to start search
int start()
int end()
Returns the current matching start position and end position
String group()
Return the current match
int groupCount()
Returns the number of groups in the input mode
int start(int groupIndex)
int end(int groupIndex)
Returns the starting position and ending position in the current match of a given group
parameter:
groupIndex group index (starting from 1), 0 means the entire match
String group(int groupIndex)
Returns a string matching a given group
parameter:
groupIndex
Grouping index (starting from 1), 0 means the entire match
String replaceAll(String replacement)
String replaceFirst(String replacement)
Returns the string obtained from the matcher input, but has replaced all or first matches with the replacement expression
parameter:
replacement Replacement Replace string
Matcher reset()
Matcher reset(CharSequence input)
Reset the master state.