Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
127 lines (92 loc) · 3.04 KB

File metadata and controls

127 lines (92 loc) · 3.04 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// A simple pattern matching demo.
import java.util.regex.*;
class RegExpr {
public static void main(String[] args) {
Pattern pat;
Matcher mat;
boolean found;
pat = Pattern.compile("Alpha");
mat = pat.matcher("Alpha");
found = mat.matches(); // check for a match
System.out.println("Testing Alpha against Alpha.");
if(found) System.out.println("Matches");
else System.out.println("No Match");
System.out.println();
System.out.println("Testing Alpha against Alpha Beta Gamma.");
mat = pat.matcher("Alpha Beta Gamma"); // create a new matcher
found = mat.matches(); // check for a match
if(found) System.out.println("Matches");
else System.out.println("No Match");
}
}
// -------------------------------------------------------
// Use find() to find a subsequence.
import java.util.regex.*;
class RegExpr2 {
public static void main(String[] args) {
Pattern pat = Pattern.compile("Alpha");
Matcher mat = pat.matcher("Alpha Beta Gamma");
System.out.println("Looking for Alpha in Alpha Beta Gamma.");
if(mat.find()) System.out.println("subsequence found");
else System.out.println("No Match");
}
}
// -------------------------------------------------------
// Use find() to find multiple subsequences.
import java.util.regex.*;
class RegExpr3 {
public static void main(String[] args) {
Pattern pat = Pattern.compile("Beta");
Matcher mat = pat.matcher("Alpha Beta Gamma Beta Theta");
while(mat.find()) {
System.out.println("Beta found at index " + mat.start());
}
}
}
// -------------------------------------------------------
// Use a quantifier.
import java.util.regex.*;
class RegExpr4 {
public static void main(String[] args) {
Pattern pat = Pattern.compile("W+");
Matcher mat = pat.matcher("W WW WWW");
while(mat.find())
System.out.println("Match: " + mat.group());
}
}
// -------------------------------------------------------
// Use wildcard and quantifier.
import java.util.regex.*;
class RegExpr5 {
public static void main(String[] args) {
Pattern pat = Pattern.compile("e.+d");
Matcher mat = pat.matcher("extend cup end table");
while(mat.find())
System.out.println("Match: " + mat.group());
}
}
// -------------------------------------------------------
// Use a character class.
import java.util.regex.*;
class RegExpr6 {
public static void main(String[] args) {
// Match lowercase words.
Pattern pat = Pattern.compile("[a-z]+");
Matcher mat = pat.matcher("this is a test.");
while(mat.find())
System.out.println("Match: " + mat.group());
}
}
// -------------------------------------------------------
// Use replaceAll().
import java.util.regex.*;
class RegExpr7 {
public static void main(String[] args) {
String str = "Jon Jonathan Frank Ken Todd";
Pattern pat = Pattern.compile("Jon.*? ");
Matcher mat = pat.matcher(str);
System.out.println("Original sequence: " + str);
str = mat.replaceAll("Eric ");
System.out.println("Modified sequence: " + str);
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.