Regular Expression
_________________________________________
_________________________________________
Any group of strings according to a particular pattern is called regular expression eg. To represent all valid mail id, by using regular expression we can validate whether the given mail id is valid or not. To represent all valid java identifiers
A pattern object represents compiled version of regular expression, we can create a pattern object by using compile() of pattern class.
Pattern p = Pattern.compile(String regularExpression);
A matcher object can be used to match character sequence against a regular expression. we can create a matcher object by using matcher() of Pattern class
Matcher m = p.matcher(String target);
boolean find() - To find next match and if it is not available return true otherwise return false
int start() - Returns start index of the match
int end() - Returns end index of the match
String group() - Returns the matched pattern
[a-z] Any lower case alphabet symbol
[A-Z] Any upper case alphabet symbol
[a-z-A-Z] Any alphabet symbol
[0-9] Any digit 0 to 9
[abc] either a or b or c
[^abc] Except a or b or c
[0-9-a-z-A-Z] Any Alpha Numeric Character
Space Character \s
[0-9] \d
[0-9-a-z-A-Z] \w
AnyCharacter .
Quantifiers - To specify number of characters to match eg.
a Exactly one a
a+ At least one a
a* Any number of a's
a? Atmost one a
Split() method
Pattern class contains method to split given String according to a regular expression. String class also contains split() to split the given string against regular expression.
Note: Pattern class split() can take target String as argument where as String class split() can take regular expression as argument
StringTokenizer - class presenting is java.util package used to divide the target String into Stream of tokens.