RegEx (Regular Expressions)
1. Make 2 regular expressions to filter a specific data set in Java and explain what they do.
2. In addition to the each regular expression, provide two test cases that will pass the input and one that will fail.
Regular expressions with test cases :
1. ".a" - In this expression the dot can ne replaced by any single character . If more than one character replaces the dot then the string will be considered invalid.
Testcase :
(a) Pattern.matches(".a","xa") - valid string.
(b) Pattern.matches(".a","xa") - invalid because we need to replace the dot and the other charachter should remain in their own positions. But here the last character is changed to x which should be a.
(c) Pattern.matches(".a","xya") - invalid because we need to replace by only a single charcater.
2. "//D" - This regular expression checks for string with non-digit characters.
Testcase :
(a) Pattern.matches(".//D" ,"abc") - Valid as it only contains alphabets.
(b) Pattern.matches(".//D" ,"9AB") - This is invalid as it contains digit along with alphabets.
Get Answers For Free
Most questions answered within 1 hours.