Regex Expression

NO. Symbol Meaning Expression Examples
1. Character
1 . Matches any character (except newline) if (“aXb”.matches(“a.b”)) acb, “aXb” ✅
2 \d Matches a single digit (0-9) if (“5”.matches(“\\d”)) 5, “0” ✅, “99” ❌
3 \D Matches a non-digit character if (“a”.matches(“\\D”)) a, “!” ✅, “1” ❌
4 \w Matches a word character (letter, digit, underscore) if (“_”.matches(“\\w”)) A, “5”, “_” ✅
5 \W Matches a non-word character if (“!”.matches(“\\W”)) !, “#”, ” ” ✅, “A” ❌
6 \s Matches a whitespace character (space, tab, newline) if (” “.matches(“\\s”))  , “\t”, “\n” ✅
7 \S Matches a non-whitespace character if (“A”.matches(“\\S”)) A, “9”, “!” ✅, ” ” ❌
         
2. Quantifiers (Repetition)
1 ? Matches 0 or 1 time (optional) if (“color”.matches(“colou?r”)) color, “colour” ✅, “colouur” ❌
2 * Matches 0 or more times if (“gooood”.matches(“go*d”)) gd, “god”, “gooood” ✅
3 + Matches 1 or more times if (“gooood”.matches(“go+d”)) god, “gooood” ✅, “gd” ❌
4 {n} Matches exactly n times if (“aaa”.matches(“a{3}”)) aaa ✅, “aa” ❌, “aaaa” ❌
5 {n,} Matches at least n times if (“aaa”.matches(“a{2,}”)) aa, “aaa” ✅, “a” ❌
6 {n,m} Matches between n and m times if (“aaa”.matches(“a{2,4}”)) aa, “aaa”, “aaaa” ✅, “aaaaa” ❌
         
3. Position Matching (Anchors)
1 ^ Matches the start of the string if (“Hello world”.matches(“^Hello.*”)) Hello world ✅, “world Hello” ❌
2 $ Matches the end of the string if (“world”.matches(“.*world$”)) Hello world ✅, “world Hello” ❌
3 \b Matches a word boundary if (“I love java”.matches(“.*\\bjava\\b.*”)) I love java ✅, “javascript” ❌
4 \B Matches a non-word boundary if (“javascript”.matches(“.*\\Bjava.*”)) javascript ✅, “java” ❌
         
4. Logical Operators
1 ` OR (match multiple options) `if (“cat”.matches(“cat|dog”)) { System.out.println(“Matched!”); }`  
2 () Capturing group (Groups part of the match) if (“gogo”.matches(“(go)+”)) { System.out.println(“Matched!”); } go, “gogo” ✅, “gg” ❌
3 (?:…) Non-capturing group (Groups without saving) if (“gogo”.matches(“(?:go)+”)) { System.out.println(“Matched!”); } gogo ✅, “gg” ❌
4 (?=…) Positive lookahead (Ensures presence but does not capture) if (“hello123”.matches(“hello(?=\\d+)”)) { System.out.println(“Matched!”); } hello123 ✅, “helloABC” ❌
5 (?!…) Negative lookahead (Ensures absence of pattern) if (“helloABC”.matches(“hello(?!\\d+)”)) { System.out.println(“Matched!”); } helloABC ✅, “hello123” ❌
6 (?<=…) Positive lookbehind (Matches with preceding text) if (“$100”.matches(“(?<=\\$)\\d+”)) { System.out.println(“Matched!”); } $100 ✅, “100” ❌
7 (?<!…) Negative lookbehind (Matches without preceding text) if (“100”.matches(“(?<!\\$)\\d+”)) { System.out.println(“Matched!”); } 100 ✅, “$100” ❌
         
5. Common Regex Patterns
1 -?\\d+  Match an integer if (“123”.matches(“-?\\d+”)) 123, “-45”, “0” ✅
2 -?\\d+(\\.\\d+)?  Match a floating point number if (“12.34”.matches(“-?\\d+(\\.\\d+)?”)) 12.34, “-0.5”, “3” ✅
3 [a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}  Match an email if (“[email protected]”.matches(“[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}”)) [email protected], “[email protected]” ✅
4 \\b(?:\\d{1,3}\\.){3}\\d{1,3}\\b Match an IPv4 address if (“192.168.1.1”.matches(“\\b(?:\\d{1,3}\\.){3}\\d{1,3}\\b”)) 192.168.1.1 ✅, “999.999.999.999” ❌
5 https?://[^\\s]+  Match a URL if (“http://example.com”.matches(“https?://[^\\s]+”)) http://example.com, “https://abc.org” ✅
Scroll to Top