FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
OnJava8-Examples/strings/TestRegularExpression.java at master · CSapprenticeWT/OnJava8-Examples · GitHub
CSapprenticeWT
OnJava8-Examples
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
OnJava8-Examples
/
strings
/
TestRegularExpression.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
44 lines (43 loc) · 1.33 KB
Breadcrumbs
OnJava8-Examples
/
strings
/
TestRegularExpression.java
Copy path
File metadata and controls
44 lines (43 loc) · 1.33 KB
Raw
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
// strings/TestRegularExpression.java
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// Simple regular expression demonstration
// {java TestRegularExpression
// abcabcabcdefabc "abc+" "(abc)+" }
import
java
.
util
.
regex
.*;
public
class
TestRegularExpression
{
public
static
void
main
(
String
[]
args
) {
if
(
args
.
length
<
2
) {
System
.
out
.
println
(
"Usage:
\n
java TestRegularExpression "
+
"characterSequence regularExpression+"
);
System
.
exit
(
0
);
}
System
.
out
.
println
(
"Input:
\"
"
+
args
[
0
] +
"
\"
"
);
for
(
String
arg
:
args
) {
System
.
out
.
println
(
"Regular expression:
\"
"
+
arg
+
"
\"
"
);
Pattern
p
=
Pattern
.
compile
(
arg
);
Matcher
m
=
p
.
matcher
(
args
[
0
]);
while
(
m
.
find
()) {
System
.
out
.
println
(
"Match
\"
"
+
m
.
group
() +
"
\"
at positions "
+
m
.
start
() +
"-"
+ (
m
.
end
() -
1
));
}
}
}
}
/* Output:
Input: "abcabcabcdefabc"
Regular expression: "abcabcabcdefabc"
Match "abcabcabcdefabc" at positions 0-14
Regular expression: "abc+"
Match "abc" at positions 0-2
Match "abc" at positions 3-5
Match "abc" at positions 6-8
Match "abc" at positions 12-14
Regular expression: "(abc)+"
Match "abcabcabc" at positions 0-8
Match "abc" at positions 12-14
*/
Back
|
FazBrowse Home
|
New Git URL