Commit a637f88
committed
feat: Add regex pattern checks for single-character and basic regex operators
📌 Purpose:
Demonstrate how regular expressions in Java match individual characters and basic string patterns using `String.matches()`.
🧪 Test Cases & Regex Logic:
1. `"."` ➝ Matches **any single character**
➤ str1 = "f" → ✅
➤ str3 = "%" → ✅
2. Literal match
➤ str2 = "8" vs `"8"` → ✅
3. `[abc]` ➝ Matches a **single character** from the set a, b, or c
➤ str4 = "abc" → ❌ (length > 1)
➤ str6 = "a" → ✅
4. `"b"` ➝ Matches **exact string** "b"
➤ str4 = "abc" → ❌
5. `"ab"` ➝ Matches string "ab" only
➤ str5 = "a" → ❌
6. `"^abc"` ➝ Anchored match for strings starting with "abc"
➤ str5 = "a" → ❌
7. `[a-z0-9]` ➝ Matches **one lowercase letter or digit**
➤ str5 = "a" → ✅
8. `[^abc]` ➝ Matches a **character not in** a, b, or c
➤ str7 = "p" → ✅
9. `[a-zA-Z0-9]` ➝ Matches alphanumeric characters
➤ str8 = "7" → ✅
🔁 Regex Operators & Character Classes:
- `a|b` ➝ Matches either "a" or "b" → str9 = "b" → ✅
- `\\w` ➝ Matches word characters `[a-zA-Z0-9_]` → str10 = "b" → ✅
- `\\d` ➝ Matches any digit → str11 = "5" → ✅
- `\\D` ➝ Matches non-digit → str12 = "$" → ✅
✅ Summary:
- Useful for validating characters and inputs like usernames, digits, or symbol filters.
- Reinforces understanding of quantifiers, character classes, negations, and alternation in Java regex.
🔧 Good foundation for expanding into multi-character and grouped pattern checks.
Signed-off-by: Somesh diwan <[email protected]>1 parent d66f768 commit a637f88
File tree
1 file changed
+39
-20
lines changed- Section6StringClassPrinting/src
1 file changed
+39
-20
lines changedLines changed: 39 additions & 20 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
| 1 | + | |
3 | 2 | | |
4 | 3 | | |
5 | 4 | | |
6 | 5 | | |
7 | 6 | | |
8 | 7 | | |
9 | 8 | | |
| 9 | + | |
| 10 | + | |
10 | 11 | | |
| 12 | + | |
| 13 | + | |
11 | 14 | | |
12 | | - | |
13 | | - | |
14 | | - | |
15 | | - | |
16 | | - | |
17 | | - | |
18 | | - | |
19 | | - | |
| 15 | + | |
| 16 | + | |
20 | 17 | | |
21 | | - | |
22 | | - | |
| 18 | + | |
| 19 | + | |
23 | 20 | | |
24 | | - | |
25 | | - | |
| 21 | + | |
| 22 | + | |
26 | 23 | | |
27 | | - | |
28 | | - | |
| 24 | + | |
| 25 | + | |
29 | 26 | | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
30 | 46 | | |
31 | | - | |
| 47 | + | |
32 | 48 | | |
| 49 | + | |
33 | 50 | | |
34 | | - | |
| 51 | + | |
35 | 52 | | |
| 53 | + | |
36 | 54 | | |
37 | | - | |
| 55 | + | |
38 | 56 | | |
| 57 | + | |
39 | 58 | | |
40 | | - | |
| 59 | + | |
41 | 60 | | |
42 | 61 | | |
0 commit comments