Java-Using Escapes

Using Escapes In regular expressions, certain characters have special meaning. This leads to the question, what if you want to search for one of those special characters? In that case, you escape the character by preceding it with a backslash. For example, in the following program, \ \( represents a left parenthesis, and \\) represents … Read more…

Java-Predefined Character Classes

Predefined Character Classes You also have a number of predefined character classes that provide you with a shorthand notation for commonly used sets of characters. Following table gives some that are particularly useful: CHARACTER CLASS DESCRIPTION \d This represents any digit and is therefore shorthand for [0-9]. \D This represents any character that is not … Read more…

Java-Capturing Groups

Capturing Groups Earlier you used the group() method for a Matcher object to retrieve the subsequence matched by the entire pattern defined by the regular expression. The entire pattern represents what is called a capturing group because the Matcher object captures the subsequence corresponding to the pattern match. Regular expressions can also define other capturing … Read more…

Java-Alternation

Alternation The vertical bar ( | ) operator denotes the logical OR operation, also called alternation or choice. The | operator does not operate on individual characters but instead applies to everything on either side of it. A regular expression to find “JAVA”,“PHP”, or “HTML” could be written as “JAVA|PHP|HTML”. Program Program Source import java.util.regex.Pattern; … Read more…