Question

Write a program using regular expression to find the starting and ending locations of ‘fox’ in...

Write a program using regular expression to find the starting and ending locations of ‘fox’ in the text 'The quick brown fox jumps over the lazy dog.' Then print out its locations as follows: Found fox from (the starting location) to (the ending location).

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

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Reg
{

     public static void main(String []args)
     {
         String data = "The quick brown fox jumps over the lazy dog";
     
         String regex = "\\bfox\\b";
        //creating an object to Pattern and passing the word to it
        Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
        //creating an object to Matcher
        Matcher matcher = pattern.matcher(data);
        //checking for given word existence in given sentence
        while (matcher.find())
            {
                //It will give index of starting letter of the word
               int s=matcher.start();
               //It will give index of ending letter of the word
               int e=matcher.end();
               System.out.print("Start index: " + s);
                System.out.print(" End index: " + e + " ");
                System.out.println(matcher.group());
                //position will come if we add one to the index
                s=s+1;
                e=e+1;
                System.out.println("\nFound fox from location "+s+" to location "+e);
  
            }
      
     }
}

Note:if you have any doubts, you can ask me.

Command Prompt :\〉javac Reg-Java D:Njava Reg Start index: 16 End index: 19 fox Found fox from location 1? to location 20

Add a comment
Know the answer?
Add Answer to:
Write a program using regular expression to find the starting and ending locations of ‘fox’ in...
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
  • Python 3:Write a program that inputs a text file. The program should print the unique words...

    Python 3:Write a program that inputs a text file. The program should print the unique words in the file in alphabetical order.   Write a program that inputs a text file. The program should print the unique words in the file in alphabetical order An example file along with the correct output is shown below: example.txt the quick brown fox jumps over the lazy dog Enter the input file name: example.txt brown dog fox jumps lazy over quick the

  • I/O program for C Write a program in direct1.c which reads from standard input a line...

    I/O program for C Write a program in direct1.c which reads from standard input a line and then outputs that line immediately to standard output. it should read the first line only ! Then, write another program called direct2.c which reads from standard input every line until end of input and outputs them to standard output. Everything that goes in should come out exactly as it was. Compile both programs and run them on the input files given below The...

  • Simple Python Program Working with strings Write a Python program to read in a number of...

    Simple Python Program Working with strings Write a Python program to read in a number of strings from the user. Stop when the user enters “Done”. Then check if each of the strings contains all the letters of the alphabet. Print the results in the form of a dictionary, where they keys are the strings and the values are the Truth Value for the string, which you just calculated. Sample Run: Enter the strings: taco cat The quick brown fox...

  • Please solve. How do I create my own text file? Where do I save it so...

    Please solve. How do I create my own text file? Where do I save it so that it is recognized by python? Do I have to import it into my code? the prompt: Write a program that inputs a text file. The program should print the unique words in the file in alphabetical order. An example file along with the correct output is shown below: example.txt the quick brown fox jumps over the lazy dog Enter the input file name:...

  • Using the website Repl.it Write a Java program that can perform the Caesar cipher for English...

    Using the website Repl.it Write a Java program that can perform the Caesar cipher for English messages that include both upper and lowercase alphabetic characters. The Caesar cipher replaces each plaintext letter with a different one, by a fixed number of places down the alphabet. The program should be able to shift a specific number of places based on the user's input number. For example Plaintext: THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG. Ciphertext: QEBNRFZH YOLTK CLU GRJMP...

  • -Brief description of the program: - input(s) and output(s) - brief description or relationship b...

    -Write in C++ language. -Brief description of the program: - input(s) and output(s) - brief description or relationship between inputs and outputs Submission Instructions: Submit only.c files in the designated Dropbox on D2L Problem 1 (25 points): Write a C program that a) reads one line (sentence) at a time from a text file called inputtext.txt (on D2L), b) reverses the words on that line, c) prints (on the screen) the modified line and d) write the modified line in...

  • C++ Write a MyString class that stores a (null-terminated) char* and a length and implements all...

    C++ Write a MyString class that stores a (null-terminated) char* and a length and implements all of the member functions below. Submit your completed source (.cpp) file. Default constructor: empty string const char* constructor: initializes data members appropriately Copy constructor: prints "Copy constructor" and endl in addition to making a copy Move constructor: prints "Move constructor" and endl in addition to moving data Copy assignment operator: prints "Copy assignment" and endl in addition to making a copy Move assignment operator:...

  • IN C++ Write a program in c++ that will read from a file the following sentence:...

    IN C++ Write a program in c++ that will read from a file the following sentence: The quick brown fox jumps over the lazy dog Each word must be read into one location in an array beginning with the first element. You must declare an array as follows: char *words [9] ; // this is an array of c- strings. HINT words[0] will contain "the" words[1] will contain "quick" write a function int length (const char *a) to determine the...

  • Regular Expression processor in Java Overview: Create a Java program that will accept a regular expression...

    Regular Expression processor in Java Overview: Create a Java program that will accept a regular expression and a filename for a text file. The program will process the file, looking at every line to find matches for the regular expression and display them. Regular Expression Format The following operators are required to be accepted: + - one or more of the following character (no groups) * - zero or more of the following character (no groups) [] – no negation,...

  • using the website repl.it (must be done in Javascript) PGM #1 Write a Java program that can perform the Caesar cipher fo...

    using the website repl.it (must be done in Javascript) PGM #1 Write a Java program that can perform the Caesar cipher for English messages that include both upper and lowercase alphabetic characters. The Caesar cipher replaces each plaintext letter with a different one, by a fixed number of places down the alphabet. The cipher illustrated here uses a left shift of three, so that (for example) each occurrence of E in the plaintext becomes B in the ciphertext. For example...

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
Active Questions
ADVERTISEMENT