Question

In: Computer Science

Please use the Java program below to test the following regex pattern (in the table) and...

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


Solutions

Expert Solution

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.


I found the text "1" starting at index 9 and ending at index 10.

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:)


Related Solutions

Java Programming Write a program that displays the following pattern *                         *       &nbsp
Java Programming Write a program that displays the following pattern *                         *          *          * *          *          *          *          *          *          *          *          *          *          *          *             *          *          *          *          *                         *          *          *                                     * Printing Pattern A * ** *** **** ***** ****** ******* Printing Pattern B ******* ****** ***** **** *** ** * Printing Pattern C * ** *** **** ***** ****** *******
Write a Java program to print the pattern of asterisks shown below. For i=1 * For...
Write a Java program to print the pattern of asterisks shown below. For i=1 * For i=2 * * * For i=3 * ** * * * For i=n * * * * * * … … … * * * * * * ……… n
Write a JAVA code using regex methods and concepts The test string S should match the...
Write a JAVA code using regex methods and concepts The test string S should match the following requirements: S must be of length 6 First character should not be a digit (1, 2, 3, 4, 5, 6, 7, 8, 9 or 0). Second character should not be a lowercase vowel (a, e, i, o or u). Third character should not be b, c, D or E. Fourth character should not be a whitespace character (\r, \n, \t, \f or the...
Java Program Use for loop 1.) Write a program to display the multiplication table of a...
Java Program Use for loop 1.) Write a program to display the multiplication table of a given integer. Multiplier and number of terms (multiplicand) must be user's input. Sample output: Enter the Multiplier: 5 Enter the number of terms: 3 5x0=0 5x1=5 5x2=10 5x3=15 2 Create a program that will allow the user to input an integer and display the sum of squares from 1 to n. Example, the sum of squares for 10 is as follows: (do not use...
Please use java language in an easy way with comments! Expand your below program, so it...
Please use java language in an easy way with comments! Expand your below program, so it can include cursed items (weapons that break, armor that causes more damage when worn). In order to do that, write an interface called "CursedItem". Then create subclasses of the armor, and weapon class that implement the CursedItem interface : CursedWeapon: using a random number generator, there is a 4 in 10 chance that the weapon breaks during combat. CursedArmor: this item amplifies the damage...
2 Write a Java program called Pattern that prints an 8-by-8 checker box pattern using an...
2 Write a Java program called Pattern that prints an 8-by-8 checker box pattern using an if loop.
This is for Java programming. Please use ( else if,) when writing the program) Write a...
This is for Java programming. Please use ( else if,) when writing the program) Write a java program to calculate the circumference for the triangle and the square shapes: The circumference for: Triangle = Side1 + Side2 +Sid3 Square = 4 X Side When the program executes, the user is to be presented with 2 choices. Choice 1 to calculate the circumference for the triangle Choice 2 to calculate the circumference for the square Based on the user's selection the...
Java. If possible please put explanations for each line of code. Write a test program that...
Java. If possible please put explanations for each line of code. Write a test program that prompts the user to enter a series of integers and displays whether the series contains runLength consecutive same-valued elements. Your program’s main() must prompt the user to enter the input size - i.e., the number of values in the series, the number of consecutive same-valued elements to match, and the sequence of integer values to check. The return value indicates whether at least one...
Write a java program StudentDriver class to test the methods of the Student class. Use data:...
Write a java program StudentDriver class to test the methods of the Student class. Use data: Mary Brown 1234 John Jones 5678 Maty Jones 1234 Note: we are including Mary Jones twice so we can test the "equals" method. We can test "compareTo", by test student1 and student2, then student2 and student3. Just submit your driver class as text file. Make sure you also test addQuiz, getTotalScore, getAverage, and toString. Don't worry about testing all of the sets and gets......
Use the below info to create a java program A GUI interface to ensure a user...
Use the below info to create a java program A GUI interface to ensure a user is old enough to play a game. Properly formatted prompts to input name, address, phone number, and age. Remember that name, address, phone number, etc. can be broken out in additional fields. Refer to the tutorial from this week’s Reading Assignment Multiple vs. Single Field Capture for Phone Number Form Input for help with this. Instructions to ensure that the information is displayed back...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT