Regular Expression in Java
In this post, we will discuss regular expression. Regular Expression is found in java.util.regex.
They are used for the pattern match in Java strings and define the search pattern for strings.
Regular Expression Syntax:
- ^ match the start of a line.
- $ match the end of the line.
- . Match every single letter instead of a new line.
- [ ] match every letter in the brackets.
- [^ ] match all the letter’s not in brackets.
- \d any digit [0-9]
- \D any non-digit [^0-9]
- \w any alpha character [a-zA-Z]
- \W any non-alphabet character [^a-zA-Z]
- \s any whitespace char
- \S nonwhite space char
Regular Expression Quantifiers:
- ? Appearing once or not at all
- + At least one occurrence
- 0 or more occurrence
- { n} exactly n occurrences
- {n,} n or more occurrences
- {n-m} at least n occurrence up to m
Split:
Let’s suppose we have a string and we want to remove the blank/white spaces from that string and want to show it in string array we use a simple string.split() function and pass empty string so that it will remove empty spaces from a string.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package myfirstproject; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; public class MyFirstProject { public static void main(String args[]) throws IOException { String data = "Ibm apple intel microsoft"; String [] splitdata = data.split(" "); for(String a:splitdata){ System.out.println(a); } } } |
Output

If we have multiple spaces in our string then this won’t be work, and we have to write \s+ as \s is used for white space and in this case, we have multiple so we use \s+ and as we have to pass a string as argument and \ is a reserved word, so we use \.
Now for blank space we use \s.
For dot (.) We use .
Replace ALL:
Now if I want to replace all the non-integer characters like alphabetic or special characters, then we use to replace all function.
1 2 3 4 5 6 7 8 9 10 |
import java.util.regex.Matcher; import java.util.regex.Pattern; public class MyFirstProject { public static void main(String args[]) throws IOException { String data = "Ibm s1a2p3p435l5e46 565i"; System.out.println(data.replaceAll("\\D", "")); } } |
Output

ReplaceAll(String regex, String replacement);

We also do this in another way like
1 |
System.out.println(data.replaceAll("[^1-9]+", "")); |
In above code ^ represent not and the scene we have multiple numbers so we add + at the last and it means it will ignore numbers 1 to 9 and replace all other numbers will null value.
And same if we do not want to replace all the alphabets then the code is:
1 |
System.out.println(data.replaceAll("[^a-zA-Z]", "")); |
Matches:
This function is used to match a string with some pattern. If it matches then it will return true otherwise false.
1 2 3 4 5 6 7 8 9 10 11 12 |
import java.util.regex.Matcher; import java.util.regex.Pattern; public class MyFirstProject { public static void main(String args[]) throws IOException { String data = "Ibm s1a2p3p435l5e46 565i"; String regix = "[^0-9]+"; System.out.println(data.matches(regix)); } } |
Output

Now what if I want to check the string only contain alphabets the regex string is
“[a-zA-Z]+”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package myfirstproject; import java.io.*; import java.math.*; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; public class MyFirstProject { public static void main(String args[]) throws IOException { String data = "12s34dv"; String regix = "[sdv2341]+"; System.out.println(data.matches(regix)); } } |
Email Validation using regex in java:
1 2 |
String email = “m7u786@mail.com”; String regex = “[@.]”; |
Code for email validation using regular expression in Java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package myfirstproject; import java.io.*; import java.math.*; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; public class MyFirstProject { public static void main(String args[]) throws IOException { String data = "m7u786@gmail.com"; String regix = "^(([a-zA-Z]|[0-9])|([-]|[_]|[.]))+[@](([a-zA-Z0-9])|([-])){2,63}[.](([a-zA-Z0-9]){2,63})+$"; System.out.println(data.matches(regix)); } } |
Output
