Question

Check the regex classes used to process regular expressions: Matcher Matching Pattern MatchPattern

Check the regex classes used to process regular expressions:

Matcher
Matching
Pattern
MatchPattern
0 0
Add a comment Improve this question Transcribed image text
Answer #1

The Regex classes used to process regular expressions were Matcher and Pattern.

Pattern Class:

Pattern class is used to define regular expressions. You can get the obect of this pattern class by
invoking compile() method which will return Pattern object.

This Pattern object allows you to create a Matcher object of a given string.

Matcher Class:

Matcher object allows you to do reqex operations on a string. To get the Matcher object you need to
invoke matcher() of Pattern class.

For Example Lets consider a string "abcdb12".

Suppose if we need to find the last 2 letters of the given string are digits then we need to create a pattern

The pattern for this should be \d{2}$ which says \d as digit from [0-9] and {} means number of occurences
an operator must match and in this case its 2 and $ means matches end of the line.

Pattern p=Pattern.compile("\\d{2}$");

Pattern obect was created and we need to pass the string "abcdb12" to the matcher as below.

Matcher m= p.matcher("abcdb12");

So the matcher and the pattern objects were created and you need to used find() method
from matcher class to verify whether last two letters were digits in the given string.

if(m.find())
{
return true;
}
else
return false;

So if its finds then this block will return true or else it will return false. In our current example it will
return true as last two letters 1,2 were digits.

Add a comment
Know the answer?
Add Answer to:
Check the regex classes used to process regular expressions: Matcher Matching Pattern MatchPattern
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT