In: Computer Science
Please use the Java program below to test the following regex pattern (in the table) and record the output, and explain how the output is generated.
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexTestBarness {
public static void main(String[]
arg) {
Scanner input = new Scanner(System.in);
while (true)
{
System.out.println("Enter a regex pattern: ");
String regex = input.nextLine();
//
Create a regex pattern
Pattern pt = Pattern.compile(regex);
System.out.println("Enter a input string to search: ");
String sr = input.nextLine();
// Create a Matcher object
Matcher mt = pt.matcher(sr);
boolean found = false;
while (mt.find()) {
System.out.printf("I found the text \"%s\" starting at " +
"index %d and ending at index
%d.\n",
mt.group(), mt.start(),
mt.end());
found = true;
}
if(!found){
System.out.println("No match found.");
}
}
}
}
regex pattern |
test string |
output |
explanation |
[^abc] |
abc |
No match found. |
Any character except a, b, or c |
GAATTC |
TGCAGAATTCGGG |
||
\d |
Homework01 |
||
\D |
2010 |
||
\s |
this is a regex |
||
\s+ |
this is a regex |
||
[A-Z][A-Z]\s\d{5} |
MD 20943 |
regex pattern | test string | output | explanation |
[^abc] | abc | No match found. | Any character except a, b, or c |
GAATTC | TGCAGAATTCGGG | I found the text "GAATTC" starting at index 4 and ending at index 10. | Find the substring in String |
\d | Homework01 |
I found the text "0" starting at index 8 and ending at index 9.
|
Find for the integer |
\D | 2010 | No match found. | Matches the nondigits |
\s | this is a regex | I found the text " " starting at index 4 and ending at index
5. I found the text " " starting at index 7 and ending at index 8. I found the text " " starting at index 9 and ending at index 10. |
Matches the whitespace |
\s+ | this is a regex | I found the text " " starting at index 4 and ending at index
5. I found the text " " starting at index 7 and ending at index 8. I found the text " " starting at index 9 and ending at index 12. |
matches the one or more spaces |
[A-Z][A-Z]\s\d{5} | MD 20943 | I found the text "MD 20943" starting at index 0 and ending at index 8. | Match Two any Character in capital followed by space and followed by 5 digit integer |
/*****************************RegexTestBarness.java************************/
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexTestBarness {
public static void main(String[] arg) {
Scanner input = new
Scanner(System.in);
while (true) {
System.out.println("Enter a regex pattern: ");
String regex =
input.nextLine();
// Create a
regex pattern
Pattern pt =
Pattern.compile(regex);
System.out.println("Enter a input string to search: ");
String sr =
input.nextLine();
// Create a
Matcher object
Matcher mt =
pt.matcher(sr);
boolean found
= false;
while
(mt.find()) {
System.out.printf("I found the text \"%s\"
starting at " + "index %d and ending at index %d.\n",
mt.group(), mt.start(), mt.end());
found = true;
}
if (!found)
{
System.out.println("No match found.");
}
}
}
}
/********************output**********************/
Please let me know if you have any doubt or modify the answer, Thanks:)