Skip to content

Commit d66f768

Browse files
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

1 file changed

+19
-8
lines changed

Section6StringClassPrinting/src/RegularExpressionString.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,30 @@ public static void main(String[] args) {
44
String str2 = "abc3";
55
String str3 = "abcccbbccdddddabcabcabcabca";
66

7-
System.out.println(str1.matches(".*"));
8-
System.out.println(str1.matches("[a-z]*"));
9-
System.out.println(str2.matches("[a-z]*"));
10-
System.out.println(str3.matches("[abc]+"));
11-
System.out.println(str3.matches("[abc]*"));
12-
System.out.println(str3.matches("[ab]{1,2}"));
7+
System.out.println("For str1 regex 1 "+str1.matches(".*"));
8+
//Match any sequence of characters (including an empty string) Will match literally anything!
139

10+
System.out.println("For str1 regex 2 "+str1.matches("[a-z]*"));
11+
// Common variations:
12+
//- `[a-zA-Z]*` - matches both lowercase and uppercase letters
13+
//- `[a-z]+` - matches one or more lowercase letters (cannot be empty)
14+
//- `[a-z]{3}` - matches exactly 3 lowercase letters
15+
// as a complete pattern means: `[a-z]*`
16+
// Match zero or more lowercase letters
17+
18+
System.out.println("For str2 regex 3 "+str2.matches("[a-z]*"));
19+
20+
System.out.println("For str3 regex 4 "+str3.matches("[abc]+"));
21+
22+
System.out.println("For str3 regex 5 "+str3.matches("[abc]*"));
23+
24+
System.out.println("For str3 regex 6"+str3.matches("[ab]{1,2}"));
1425

1526

1627
String str4="[email protected]";
1728
String str5="[email protected]";
1829

19-
System.out.println(str4.matches(".*gmail.*"));
20-
System.out.println(str5.matches("\\w*@gmail(.*)"));
30+
System.out.println("For str4 regex 7 "+str4.matches(".*gmail.*"));
31+
System.out.println("For str5 regex 8 "+str5.matches("\\w*@gmail(.*)"));
2132
}
2233
}

0 commit comments

Comments
 (0)