-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegexIntroduction.java
More file actions
199 lines (185 loc) · 7.72 KB
/
Copy pathRegexIntroduction.java
File metadata and controls
199 lines (185 loc) · 7.72 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package Phase6_RuntimeMemoryRegexReflection.Regex;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Regex in Java - Introduction
* ----------------------------
* A REGULAR EXPRESSION (regex) is a small language for describing
* patterns of text. Java exposes it through the package java.util.regex,
* which contains two main classes:
* <p>
*
* Pattern - the compiled representation of a regex.
* Matcher - the engine that runs a Pattern against an input.
* <p>
*
* Pattern p = Pattern.compile("\\d+");
* Matcher m = p.matcher("abc 123 xyz 45");
* m.find(); // true; matched "123"
* m.group(); // "123"
* <p>
*
* What Can A Regex Do?
* --------------------
* - VALIDATE a string fits a shape (e.g. an email address).
* - SEARCH for occurrences inside a longer text.
* - EXTRACT pieces of a string into named groups.
* - REPLACE one substring (or set of substrings) with another.
* - SPLIT a string at every occurrence of a delimiter.
* <p>
*
* The 3 Ways to Use Regex in Java
* -------------------------------
* 1. Convenience methods on java.lang.String:
* <p>
*
* "abc 123".matches("\\w+ \\d+");
* "a,b,,c".split(",");
* "x1y2z3".replaceAll("\\d", "*");
* "x1y2z3".replaceFirst("\\d", "*");
* <p>
*
* These compile the regex EACH call. Fine for one-shots, wasteful
* inside loops.
* <p>
*
* 2. Pattern + Matcher - precompile once, reuse many times:
* <p>
*
* Pattern p = Pattern.compile("\\d+");
* for (String s : huge) {
* Matcher m = p.matcher(s);
* ...
* }
* <p>
*
* 3. The newer Stream/functional API (Java 8+):
* <p>
*
* p.matcher(text).results().map(MatchResult::group).forEach(...);
* p.splitAsStream(text);
* <p>
*
* Important: matches() vs find()
* ------------------------------
* m.matches() - returns TRUE only if the ENTIRE input matches.
* m.find() - returns TRUE if a SUBSTRING anywhere matches.
* m.lookingAt()- returns TRUE if the input matches starting AT THE BEGINNING
* (does not need to consume the whole input).
* <p>
*
* `String.matches(regex)` calls `matches()` - so it requires a full match.
* That is the #1 source of "my regex doesn't match" surprise.
* <p>
*
* Escaping In Java Strings
* ------------------------
* Regex uses `\` for many meta-characters (\d, \s, \w, \., \(, ...). Java
* strings ALSO use `\` for escapes. That means a regex `\d` is written
* `"\\d"` in a Java string literal. A literal backslash in regex is `\\`,
* which is `"\\\\"` in a string literal.
* <p>
*
* Java 15+ TEXT BLOCKS spare you one level of escaping:
* <p>
*
* String r = """
* \d+\s+\w+
* """.strip();
* <p>
*
* Folder Map
* ----------
* RegexIntroduction.java (this file)
* MatcherClass.java - everything Matcher can do
* CharacterClass.java - [...], \d, \w, \s, POSIX, Unicode
* Quantifiers.java - *, +, ?, {n,m} and greedy/reluctant/possessive
* MetacharactersAndAnchors.java
* - ^, $, \b, \B, ., escapes
* GroupsAndBackreferences.java
* - (...), (?:...), (?<name>...), \1
* LookaroundAssertions.java - (?=...), (?!...), (?<=...), (?<!...)
* RegexFlags.java - CASE_INSENSITIVE / MULTILINE / DOTALL / ...
* ModernRegexFeatures.java - Stream-based regex (Java 8+)
* RegexExamples.java - email / phone / url / password / etc.
*/
public class RegexIntroduction {
public static void main(String[] args) {
section("1) The shortcuts on java.lang.String");
System.out.println("\"abc 123\".matches(\\\\w+ \\\\d+) = " + "abc 123".matches("\\w+ \\d+"));
System.out.println("split(\",\") = " + java.util.Arrays.toString("a,b,,c".split(",")));
System.out.println("replaceAll(d->*) = " + "x1y2z3".replaceAll("\\d", "*"));
System.out.println("replaceFirst = " + "x1y2z3".replaceFirst("\\d", "*"));
section("2) Pattern + Matcher - precompile once, reuse many times");
Pattern wordPlusDigits = Pattern.compile("(\\w+)(\\d+)");
String[] inputs = {"alpha42", "x99", "noDigits", "z00"};
for (String in : inputs) {
Matcher m = wordPlusDigits.matcher(in);
if (m.matches()) {
System.out.println(in + " -> letters='" + m.group(1) + "', digits='" + m.group(2) + "'");
} else {
System.out.println(in + " -> no full match");
}
}
section("3) matches() vs find() vs lookingAt()");
Pattern digits = Pattern.compile("\\d+");
Matcher m = digits.matcher("abc 123 xyz 45");
System.out.println("matches() = " + m.matches()); // false - full match required
m.reset();
System.out.println("lookingAt() = " + m.lookingAt()); // false - input does NOT start with digits
m.reset();
System.out.println("find() = " + m.find()); // true - "123"
System.out.println("group() = " + m.group());
// .find() can be called repeatedly to walk through every match
m.reset();
System.out.print("all matches via find(): ");
while (m.find()) System.out.print(m.group() + " ");
System.out.println();
section("4) Java escape vs regex escape - keep them straight");
// To MATCH a literal dot, the regex needs `\.` ; the Java string needs `\\.`
Pattern dot = Pattern.compile("\\.");
System.out.println("\"3.14\".split(dot) = " + java.util.Arrays.toString(dot.split("3.14")));
// versus
System.out.println("\"3.14\".split(.) = " + java.util.Arrays.toString("3.14".split("."))); // splits EVERYTHING
section("5) Compilation cost - reuse a Pattern when looping");
long t0 = System.nanoTime();
for (int i = 0; i < 10_000; i++) {
"x42".matches("\\w+\\d+"); // compiles every call
}
long slow = (System.nanoTime() - t0) / 1_000_000;
Pattern compiled = Pattern.compile("\\w+\\d+");
t0 = System.nanoTime();
for (int i = 0; i < 10_000; i++) {
compiled.matcher("x42").matches(); // compiled once
}
long fast = (System.nanoTime() - t0) / 1_000_000;
System.out.println("String.matches loop : " + slow + " ms");
System.out.println("Precompiled loop : " + fast + " ms");
// SAMPLE OUTPUT
// ====== 1) The shortcuts on java.lang.String ======
// "abc 123".matches(\\w+ \\d+) = true
// split(",") = [a, b, , c]
// replaceAll(d->*) = x*y*z*
// replaceFirst = x*y2z3
// ====== 2) Pattern + Matcher - precompile once, reuse many times ======
// alpha42 -> letters='alpha', digits='42'
// x99 -> letters='x', digits='99'
// noDigits -> no full match
// z00 -> letters='z', digits='00'
// ====== 3) matches() vs find() vs lookingAt() ======
// matches() = false
// lookingAt() = false
// find() = true
// group() = 123
// all matches via find(): 123 45
// ====== 4) Java escape vs regex escape - keep them straight ======
// "3.14".split(dot) = [3, 14]
// "3.14".split(.) = [] <- the . matches every char, leaving only empties
// ====== 5) Compilation cost - reuse a Pattern when looping ======
// String.matches loop : 18 ms
// Precompiled loop : 3 ms
}
private static void section(String title) {
System.out.println("\n====== " + title + " ======");
}
}