RegEx (Regular expression) tutorial with examples in java



Numeric check >

1. how to check string contains numeric value in java using Regex(Regular Expression) program   

2. How to check string contains ONLY numeric value in Java using Regex(Regular Expression) program



Alphabet check >

3. How to check string contains at least one alphabets in Java using Regex(Regular Expression) program

4. How to check string contains ONLY alphabets/letters in Java using Regex(Regular Expression) program


Special character check >

5. How to check string contains special characters in Java using Regex(Regular Expression) program

6. How to check string contains ONLY special characters in Java using Regex(Regular Expression) program


Alpha numeric check >

7. How to check string is alphanumeric or not in Java using Regex(Regular Expression) program





1. How to check string contains numeric value using regex (regular expression) in java.


Regular expressions provides one of the simplest ways to find whether string contains numeric value or not in java.


Example >
string 'java7' contains numeric value
string 'java@&8' contains numeric value
string '123' contains numeric value


string 'java' doesn't contains numeric value
string 'java@&' doesn't contains numeric value

Program/Example 1 to check string contains numeric value in java using regex >
regex (Regular Expression) used = ".*\\d.*"
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class StringContainsNumericExample1 {
public static void main(String[] args) {
     Pattern pattern = Pattern.compile(".*\\d.*");
     String str = "java7";
     Matcher matcher = pattern.matcher(str);
     if (matcher.matches()) {
          System.out.println("string '"+str + "' contains numeric value");
     } else {
          System.out.println("string '"+str + "' doesn't contains numeric value");
     }
}
}
/*
string 'java7' contains numeric value
*/


Program/Example 2 to check string contains numeric value in java using regex >
regex (Regular Expression) used = "[a-zA-Z ]*\\d+.*"
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class StringContainsNumericExample2 {
public static void main(String[] args) {
     Pattern pattern = Pattern.compile("[a-zA-Z ]*\\d+.*");
     String str = "java7";
     Matcher matcher = pattern.matcher(str);
     if (matcher.matches()) {
          System.out.println("string '"+str + "' contains numeric value");
     } else {
          System.out.println("string '"+str + "' doesn't contains numeric value");
     }
}
}
/*
string 'java7' contains numeric value
*/




2. How to check string contains only numeric value using regex (regular expression) in java.


Regular expressions provides one of the simplest ways to find whether string contains only numeric value or not in java.


Example >
string '123' contains only numeric value
string '12345' contains only numeric value


string '123e' doesn't contains only numeric value
string '12a3' doesn't contains only numeric value
string '123@#' doesn't contains only numeric value

Program/Example 1 to check string contains only numeric value in java using regex >
regex (Regular Expression) used = "\\d+"
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class StringContainsOnlyNumericExample1 {
public static void main(String[] args) {
     Pattern pattern = Pattern.compile("\\d+");
     String str = "1234";
     Matcher matcher = pattern.matcher(str);
     if (matcher.matches()) {
          System.out.println("string '"+str + "' contains only numeric value");
     } else {
          System.out.println("string '"+str + "' doesn't contains only numeric value");
     }
}
}
/*
string '1234' contains only numeric value
*/


Program/Example 2 to check string contains only numeric value in java using regex >
regex (Regular Expression) used = "[0-9]+"
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class StringContainsOnlyNumericExample2 {
public static void main(String[] args) {
     Pattern pattern = Pattern.compile("[0-9]+");
     String str = "1234";
     Matcher matcher = pattern.matcher(str);
     if (matcher.matches()) {
          System.out.println("string '"+str + "' contains only numeric value");
     } else {
          System.out.println("string '"+str + "' doesn't contains only numeric value");
     }
}
}
/*
string '1234' contains only numeric value
*/


Program/Example 3 to check string contains only numeric value in java using org.apache.commons.lang.StringUtils.isNumeric(str) >
import org.apache.commons.lang.StringUtils;
public class StringContainsOnlyNumericExample3_UsingCommons {
public static void main(String[] args) {
     String str = "1234";
     System.out.println(StringUtils.isNumeric(str));
}
}
/*
true
*/


Jar used in above program = commons-lang.jar


3. How to check string contains alphabets/letters using regex (regular expression) in java.


Regular expressions provides one of the simplest ways to find whether string contains at least one alphabets/letters or not in java.


Example >
string 'a123' contains at least one alphabets/letters
string 'abcd' contains at least one alphabets/letters
string '@!ab' contains at least one alphabets/letters


string '1234' doesn't contains any alphabets/letters value
string '@#12!' doesn't contains any alphabets/letters value

Program/Example 1 to check string contains alphabets/letters in java using regex >
regex (Regular Expression) used = ".*[a-zA-Z]+.*"
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class StringContainsAlphabetsExample {
public static void main(String[] args) {
     Pattern pattern = Pattern.compile(".*[a-zA-Z]+.*");
     String str = "a123";
     Matcher matcher = pattern.matcher(str);
     if (matcher.matches()) {
          System.out.println("string '"+str + "' contains at least one alphabets/letters");
     } else {
          System.out.println("string '"+str + "' doesn't contains any alphabets/letters value");
     }
}
}
/*
string 'a123' contains at least one alphabets/letters
*/



4. How to check string contains only alphabets/letters using regex (regular expression) and apache commons StringUtils class in java.


Regular expressions provides one of the simplest ways to find whether string contains only alphabets/letters or not in java.


Example >
string 'abcd' contains only alphabets/letters
string 'abqu' contains only alphabets/letters


string 'ab12' doesn't contains only alphabets/letters value
string 'ab@' doesn't contains only alphabets/letters value

Program/Example 1 to check string contains only alphabets/letters in java using regex >
regex (Regular Expression) used = "[a-zA-Z]+"
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class StringContainsOnlyAlphabetsExample1 {
public static void main(String[] args) {
     Pattern pattern = Pattern.compile("[a-zA-Z]+");
     String str = "abcd";
     Matcher matcher = pattern.matcher(str);
     if (matcher.matches()) {
          System.out.println("string '"+str + "' contains only alphabets/letters");
     } else {
          System.out.println("string '"+str + "' doesn't contains only alphabets/letters value");
     }
}
}
/*
string 'abcd' contains only alphabets/letters value
*/


Program/Example 2 to check string contains only alphabets/letters in java using org.apache.commons.lang.StringUtils.isAlpha(str) >
import org.apache.commons.lang.StringUtils;
public class StringContainsOnlyAlphabetsExample2_UsingCommons {
public static void main(String[] args) {
     String str = "abcd";
     System.out.println(StringUtils.isAlpha(str));
}
}
/*
true
*/


Jar used in above program = commons-lang.jar


5. How to check string contains special characters using regex (regular expression) in java.


Regular expressions provides one of the simplest ways to find whether string contains special characters or not in java.


Example >
string 'abc@' contains special character
string '1#23' contains special character
string '1#@a' contains special character
string '%&#@' contains special character


string 'abc' doesn't contains special character
string 'abc12' doesn't contains special character
string '123' doesn't contains special character

Program/Example to check string contains special characters in java using regex >


Any string that doesn’t matches regex "[a-zA-Z0-9]*" contains special characters.
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class StringContainsSpecialCharactersExample {
public static void main(String[] args) {
     Pattern pattern = Pattern.compile("[a-zA-Z0-9]*");
     String str = "abc@";
     Matcher matcher = pattern.matcher(str);
     if (!matcher.matches()) {
          System.out.println("string '"+str + "' contains special character");
     } else {
          System.out.println("string '"+str + "' doesn't contains special character");
     }
}
}
/*
string 'abc@' contains special character
*/


6. How to check string only contains special characters using regex (regular expression) in java.


Regular expressions provides one of the simplest ways to find whether string contains only special characters or not in java.


Example >
string '@#!' contains only special character
string '@#!{}$' contains only special character
string '#*^!' contains only special character


string '12@#' doesn't contains only special character
string 'ab@#' doesn't contains only special character
string 'abcd' doesn't contains only special character
string 'ab12' doesn't contains only special character

Program/Example to check string contains only special characters in java using regex >


public class StringContainsOnlySpecialCharactersExample {
public static void main(String[] args) {
     String str = "@#!";
     String specialCharacters = "[" + "-/@#!*$%^&.'_+={}()"+ "]+" ;
    
     if ( str.matches(specialCharacters)) {
          System.out.println("string '"+str + "' contains only special character");
     } else {
          System.out.println("string '"+str + "' doesn't contains only special character");
     }
}
}
/*
string '@#!' contains only special character
*/



7. how to check string is alphanumeric or not using regex (regular expression) and apache commons StringUtils class  in java.


Regular expressions provides one of the simplest ways to find whether string is alphanumeric or not in java.


Example >
string 'ab12' is alphanumeric
string '123' is alphanumeric
string 'abc' is alphanumeric


string '!#@' isn't alphanumeric
string 'abc@' isn't alphanumeric
string 'abc12#' isn't alphanumeric


Example 1 to check string is alphanumeric or not in java using regex >
regex (Regular Expression) used = "[a-zA-Z0-9]*"
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class StringAlphaNumericExample {
public static void main(String[] args) {
     Pattern pattern = Pattern.compile("[a-zA-Z0-9]*");
     String str = "abc12";
     Matcher matcher = pattern.matcher(str);
     if (matcher.matches()) {
          System.out.println("string '"+str + "' is alphanumeric");
     } else {
          System.out.println("string '"+str + "' isn't alphanumeric");
     }
}
}
/*
string 'abc12' is alphanumeric
*/


Example 2 to check string is alphanumeric or not in java using org.apache.commons.lang.StringUtils.isAlphanumeric(str) >
import org.apache.commons.lang.StringUtils;
public class StringContainsAlphaNumericExample2_UsingCommons {
public static void main(String[] args) {
     String str = "abc123";
     System.out.println(StringUtils.isAlphanumeric(str));
}
}
/*
true
*/


Jar used in above program = commons-lang.jar

eEdit
Must read for you :