Question: What are the Pattern and Matcher classes in java and how do they differ?

My second question is, can you explain the following validation code clearly?

```
Pattern p = Pattern.compile("^[a-zA-Z][a-zA-Z\\s]+$");
Pattern pattern = Pattern.compile("\\d{10}");

```

Answer: 
> 
> **`Matcher`** --> A Matcher is created from a Pattern by invoking the
>  Pattern's matcher method. The Matcher will match the given pattern (used to create it) against the String to be matched.
> 
> 
> **`Pattern`** --> A compiled representation of a regular expression. A
>  regular expression, specified as a string, must first be compiled into
>  an instance of this class.
> 
> 
> 

```
Pattern p = Pattern.compile("^[a-zA-Z][a-zA-Z\\s]+$"); 
// creates a regex pattern that can match a character followed by one or more characters or space
example : `ab` or `asa[space]` but not `a2` or `a` or `2`

Pattern pattern = Pattern.compile("\\d{10}");
// creates a regex pattern which can match exactly 10 digits
example : 1234567890

```
And just to clarify, the Matcher class does **not** have a constructor. It is constructed from the Pattern class. So you will always need a Pattern object first. 

But once the Matcher object is created it will be your "*worker*". You will typically use methods off the Matcher object like **.find()** or **.group()**, etc.

Check out the java docs on the Matcher class. <http://docs.oracle.com/javase/7/docs/api/java/util/regex/Matcher.html>