Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can someone help me with this java regex problem? Thanks Directions: Write one j

ID: 3678596 • Letter: C

Question

Can someone help me with this java regex problem? Thanks

Directions:
Write one java program that matches the following text strings.
You will write a single java program that reads in the Jabberwocky.txt file.
It will then match the specified text, print the regex you used to match that text then the text that was matched.
It will do this for each of the eight patterns.
You must comment you code and your code must have header blocks.

1:
Jabberwocky
“Beware the Jabberwock, my son!
The Jabberwock, with eyes of flame,
“And hast thou slain the Jabberwock?
2:
And, as in uffish thought he stood,
The Jabberwock, with eyes of flame,
Came whiffling through the tulgey wood,
He left it dead, and with its head
3:
BY LEWIS CARROLL
So rested he by the Tumtum tree
4:
Jabberwocky
BY LEWIS CARROLL
’Twas brillig, and the slithy toves
All mimsy were the borogoves,
“Beware the Jabberwock, my son!
Beware the Jubjub bird, and shun
He took his vorpal sword in hand;
So rested he by the Tumtum tree
And, as in uffish thought he stood,
Came whiffling through the tulgey wood,
One, two! One, two! And through and through
He left it dead, and with its head
“And hast thou slain the Jabberwock?
O frabjous day! Callooh! Callay!”
’Twas brillig, and the slithy toves
All mimsy were the borogoves,
5:
Jabberwocky
“Beware the Jabberwock, my son!
“And hast thou slain the Jabberwock?
O frabjous day! Callooh! Callay!”
6:
Jabberwocky
“Beware the Jabberwock, my son!
“And hast thou slain the Jabberwock?
7: (Yes, this is spaced correctly)
Did gyre and gimble in the wabe:
The frumious Bandersnatch!”
Long time the manxome foe he sought—
Did gyre and gimble in the wabe:
8:
“Beware the Jabberwock, my son!
The Jabberwock, with eyes of flame,

Explanation / Answer

import java.io.*; import java.util.regex.*; import java.util.*; import com.bruceeckel.util.*; public class JGrep { public static void main(String[] args) throws Exception { if(args.length < 2) { System.out.println("Usage: java JGrep file regex"); System.exit(0); } Pattern p = Pattern.compile(args[1]); // Iterate through the lines of the input file: ListIterator it = new TextFile(args[0]).listIterator(); while(it.hasNext()) { Matcher m = p.matcher((String)it.next()); while(m.find()) System.out.println(it.nextIndex() + ": " + m.group() + ": " + m.start()); } } }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote