Question

How can direct string matching be changed to work with regular expressions?

How can direct string matching be changed to work with regular expressions?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Given below is a Java code which will demonstrate how direct string matching be changed to work with regular expressions?

1. DirectStringMatching.java

import java.util.regex.Pattern;

public class DirectStringMatching {

public boolean isStringDirectlyMatched(final String str1, final String str2){

return (str1.equals(str2) ? true : false); // return true if both string1 and string2 are same

}

public boolean isStringPatternMatched(final String str1, final String pattern){

return (Pattern.matches(pattern, str1) ? true : false); // return true is str1 matches Pattern: pattern

}

public static void main(String[] args) {

DirectStringMatching directStringMatchingObj = new DirectStringMatching();

String str1, str2, pattern;

str1 = "Gopal Malaker";

str2 = "Gopal Malaker";

System.out.println("str1 and str2 are matched: " + directStringMatchingObj.isStringDirectlyMatched(str1, str2));

str2 = "Chegg";

System.out.println("str1 and str2 are matched: " + directStringMatchingObj.isStringDirectlyMatched(str1, str2));

str1 = "GopalMalaker";

pattern = "[a-z|A-Z|0-9]*";

System.out.println("str1 and pattern are matched: " + directStringMatchingObj.isStringPatternMatched(str1, pattern));

}

}

The Output is attached below:-

Console x <terminated> DirectStringMatching [Java Application] C:\Program FilesVava jdk1.8.0_121\bin\javaw.exe (Apr str1 and

Please let me know in case of any clarifications required. Thanks!

Add a comment
Know the answer?
Add Answer to:
How can direct string matching be changed to work with regular expressions?
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