http://www.technicalpage.net/search/label/SQL

Regular Expression

Regular Expression
 
Suppose we have a string = aaMsN12@uT&5#9z12$
Using Regular Expression, Let's get/print/extract:
- Text/characters only
- Upper characters only
- Lower characters only
- Numbers only
- Special characters only

package packageName;

public class RegularExpression {

       public static void main(String[] args) {  

             String str = "aaMsN12@uT&5#9z12$";

             String numOnly = str.replaceAll("[^0-9a-z]","");

             System.out.println("Characters/Text only = "+str.replaceAll("[^a-zA-Z]",""));

             System.out.println("small characters only = "+str.replaceAll("[^a-z]",""));

             System.out.println("Upper characters only = "+str.replaceAll("[^A-Z]",""));

             System.out.println("Numbers only = "+str.replaceAll("[^0-9]",""));

             System.out.println("Special Characters only = "+str.replaceAll("[a-zA-Z0-9]",""));

       }

}

 

Output:

Characters/Text only = aaMsNuTz

small characters only = aasuz

Upper characters only = MNT

Numbers only = 125912

Special Characters only = @&#$

No comments:

Post a Comment