Monday, January 31, 2011

How to use regular Expressins in String manipulation

Regular expressions are power full and efficient. i used them recently for my project and here i will just show few usages of it

Its so simple

Pattern p = Pattern.compile(regexPatternString);
Matcher m = p.matcher(textString);

then evaluate using
 //write what ever if that match you want to do

//pattern to get character contains only 1 to 3(1,2,3) and letter c( C and c)
Pattern p = Pattern.compile("[.1-3cC]");

//only Numerical values string that contains only digits and any number of digits
Pattern p = Pattern.compile(("[0-9].*")

//get string that only contains "sanjeewa" or "malalgoda"
Pattern p = Pattern.compile(("sanjeewa|malalgoda")

No comments:

Post a Comment