Skip to content

Commit a637f88

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

1 file changed

+39
-20
lines changed
Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,61 @@
1-
public class RegularExpressionStringAB
2-
{
1+
public class RegularExpressionStringAB {
32
public static void main(String[] args) {
43
String str1 = "f";
54
String str2 = "8";
65
String str3 = "%";
76
String str4 = "abc";
87
String str5 = "a";
98

9+
// "." matches any single character
10+
System.out.println(str1.matches(".")); // true - matches any single character
1011

12+
// Literal match - matches exactly "8"
13+
System.out.println(str2.matches("8")); // true - exact match
1114

12-
System.out.println(str1.matches("."));
13-
System.out.println(str2.matches("8"));
14-
System.out.println(str3.matches("."));
15-
System.out.println(str4.matches("[abc]"));
16-
System.out.println(str4.matches("b"));
17-
System.out.println(str5.matches("ab"));
18-
System.out.println(str5.matches("^abc"));
19-
System.out.println("Hmm " + str5.matches("[a-z0-9]"));
15+
// "." matches any single character
16+
System.out.println(str3.matches(".")); // true - matches any single character
2017

21-
String str6="a"; //b,c
22-
System.out.println(str6.matches("[abc]"));
18+
// [abc] matches single character that is either a, b, or c
19+
System.out.println(str4.matches("[abc]")); // false - matches only single character from set
2320

24-
String str7="p"; //true for alphabets other than abc
25-
System.out.println(str7.matches("[^abc]"));
21+
// Literal match - matches exactly "b"
22+
System.out.println(str4.matches("b")); // false - str4 is "abc", not "b"
2623

27-
String str8="7";//A,a7
28-
System.out.println(str8.matches("[a-zA-Z0-9]"));//[a-z][0-9]
24+
// Matches exactly "ab"
25+
System.out.println(str5.matches("ab")); // false - str5 is "a", not "ab"
2926

27+
// ^abc matches string that starts with "abc"
28+
System.out.println(str5.matches("^abc")); // false - str5 is "a", not starting with "abc"
29+
30+
// [a-z0-9] matches single character that is either lowercase letter or digit
31+
System.out.println("Hmm " + str5.matches("[a-z0-9]")); // true - 'a' is in range a-z
32+
33+
// [abc] matches single character from the set {a,b,c}
34+
String str6="a";
35+
System.out.println(str6.matches("[abc]")); // true - 'a' is in the set
36+
37+
// [^abc] matches single character that is NOT a, b, or c
38+
String str7="p";
39+
System.out.println(str7.matches("[^abc]")); // true - 'p' is not in {a,b,c}
40+
41+
// [a-zA-Z0-9] matches single character that is letter (any case) or digit
42+
String str8="7";
43+
System.out.println(str8.matches("[a-zA-Z0-9]")); // true - '7' is a digit
44+
45+
// a|b matches either "a" or "b"
3046
String str9="b";
31-
System.out.println(str9.matches("a|b"));
47+
System.out.println(str9.matches("a|b")); // true - str9 is "b"
3248

49+
// \w matches any word character (letter, digit, or underscore)
3350
String str10="b";
34-
System.out.println(str10.matches("\\w"));
51+
System.out.println(str10.matches("\\w")); // true - 'b' is a word character
3552

53+
// \d matches any digit
3654
String str11="5";
37-
System.out.println(str11.matches("\\d"));
55+
System.out.println(str11.matches("\\d")); // true - '5' is a digit
3856

57+
// \D matches any non-digit character
3958
String str12="$";
40-
System.out.println(str12.matches("\\D"));
59+
System.out.println(str12.matches("\\D")); // true - '$' is not a digit
4160
}
4261
}

0 commit comments

Comments
 (0)