Commit d66f768
committed
feat: Add multiple regex pattern checks on string content and format matching
📌 Objective:
Demonstrate how various regular expressions (`regex`) operate on different string inputs using `String.matches()` method in Java.
🧪 Tests & Logic Implemented:
1. `str1.matches(".*")`
✔ Matches *any* character sequence (including empty).
➤ Output: true
2. `str1.matches("[a-z]*")`
✔ Matches zero or more lowercase letters only.
➤ Output: true (since str1 = "abcdf")
3. `str2.matches("[a-z]*")`
✖ Should fail due to presence of digit '3'
➤ Output: false
4. `str3.matches("[abc]+")`
✔ Matches one or more characters from a, b, or c only
➤ Output: false (contains 'd')
5. `str3.matches("[abc]*")`
✔ Matches zero or more of a, b, or c (no other characters)
➤ Output: false (due to 'd')
6. `str3.matches("[ab]{1,2}")`
✖ Fails because it only allows 1–2 characters of a or b, but str3 is much longer
➤ Output: false
7. `str4.matches(".*gmail.*")`
✔ True if 'gmail' exists anywhere in the string
➤ Output: true
8. `str5.matches("\\w*@gmail(.*)")`
✔ True if format is a word-character-only username + "@gmail" + optional characters
➤ Output: true
🎯 Highlights:
- Demonstrates matching character classes `[a-z]`, quantifiers like `*`, `+`, and `{1,2}`, and string anchors.
- Shows real-world pattern usage like email format detection.
- Helps understand when regex patterns fail due to extra characters or length mismatch.
✅ Useful for:
- Regex learners
- Input validation
- Email pattern checks
- Character-level filtering and logic testing
Signed-off-by: Somesh diwan <[email protected]>1 parent efbd471 commit d66f768
File tree
1 file changed
+19
-8
lines changed- Section6StringClassPrinting/src
1 file changed
+19
-8
lines changedLines changed: 19 additions & 8 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
7 | | - | |
8 | | - | |
9 | | - | |
10 | | - | |
11 | | - | |
12 | | - | |
| 7 | + | |
| 8 | + | |
13 | 9 | | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
14 | 25 | | |
15 | 26 | | |
16 | 27 | | |
17 | 28 | | |
18 | 29 | | |
19 | | - | |
20 | | - | |
| 30 | + | |
| 31 | + | |
21 | 32 | | |
22 | 33 | | |
0 commit comments