FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
OnJava8-Examples/strings/StartEnd.java at master · xinan-w/OnJava8-Examples · GitHub
xinan-w
/
OnJava8-Examples
Public
forked from
BruceEckel/OnJava8-Examples
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
OnJava8-Examples
/
strings
/
StartEnd.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
82 lines (81 loc) · 2.5 KB
Breadcrumbs
OnJava8-Examples
/
strings
/
StartEnd.java
Copy path
File metadata and controls
82 lines (81 loc) · 2.5 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
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
// strings/StartEnd.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.
import
java
.
util
.
regex
.*;
public
class
StartEnd
{
public
static
String
input
=
"As long as there is injustice, whenever a
\n
"
+
"Targathian baby cries out, wherever a distress
\n
"
+
"signal sounds among the stars "
+
"... We'll be there.
\n
"
+
"This fine ship, and this fine crew ...
\n
"
+
"Never give up! Never surrender!"
;
private
static
class
Display
{
private
boolean
regexPrinted
=
false
;
private
String
regex
;
Display
(
String
regex
) {
this
.
regex
=
regex
; }
void
display
(
String
message
) {
if
(!
regexPrinted
) {
System
.
out
.
println
(
regex
);
regexPrinted
=
true
;
}
System
.
out
.
println
(
message
);
}
}
static
void
examine
(
String
s
,
String
regex
) {
Display
d
=
new
Display
(
regex
);
Pattern
p
=
Pattern
.
compile
(
regex
);
Matcher
m
=
p
.
matcher
(
s
);
while
(
m
.
find
())
d
.
display
(
"find() '"
+
m
.
group
() +
"' start = "
+
m
.
start
() +
" end = "
+
m
.
end
());
if
(
m
.
lookingAt
())
// No reset() necessary
d
.
display
(
"lookingAt() start = "
+
m
.
start
() +
" end = "
+
m
.
end
());
if
(
m
.
matches
())
// No reset() necessary
d
.
display
(
"matches() start = "
+
m
.
start
() +
" end = "
+
m
.
end
());
}
public
static
void
main
(
String
[]
args
) {
for
(
String
in
:
input
.
split
(
"
\n
"
)) {
System
.
out
.
println
(
"input : "
+
in
);
for
(
String
regex
:
new
String
[]{
"
\\
w*ere
\\
w*"
,
"
\\
w*ever"
,
"T
\\
w+"
,
"Never.*?!"
})
examine
(
in
,
regex
);
}
}
}
/* Output:
input : As long as there is injustice, whenever a
\w*ere\w*
find() 'there' start = 11 end = 16
\w*ever
find() 'whenever' start = 31 end = 39
input : Targathian baby cries out, wherever a distress
\w*ere\w*
find() 'wherever' start = 27 end = 35
\w*ever
find() 'wherever' start = 27 end = 35
T\w+
find() 'Targathian' start = 0 end = 10
lookingAt() start = 0 end = 10
input : signal sounds among the stars ... We'll be
there.
\w*ere\w*
find() 'there' start = 43 end = 48
input : This fine ship, and this fine crew ...
T\w+
find() 'This' start = 0 end = 4
lookingAt() start = 0 end = 4
input : Never give up! Never surrender!
\w*ever
find() 'Never' start = 0 end = 5
find() 'Never' start = 15 end = 20
lookingAt() start = 0 end = 5
Never.*?!
find() 'Never give up!' start = 0 end = 14
find() 'Never surrender!' start = 15 end = 31
lookingAt() start = 0 end = 14
matches() start = 0 end = 31
*/
Back
|
FazBrowse Home
|
New Git URL