CREATE A PROGRAM THAT SEARCHES A TEXT FILE BY USING REGULAR EXPRESSIONS.
SOLUTION
Create a simple applications that will loop through a text file(gettys.html) and search for
text by using regular expresions. If the desired text found on a line, print out the line number
and the line text.
9 <h4>Abrahan Lincoln</h4>
10 <h4>Thrusday, November 19, 1863</h4>
Tasks
The gettys.html file is located in the root of the project folder. To examine the file,
with the project open, click the file tab. Double-click the file to open it and exmaine its
content.
1. Edit teh FindText.java file.
2. Create a pattern and a Matcher field.
3. Generate a Matcher based on the supplied Pattern object.
4. Search each line for the pattern supplied.
5. Print the line numver and the line that has matching text.
6. Run the FindText.java file and search for these patterns.
All lines that contain <h4>
All the ines that contain the word "to" (For example, line 17 should
not be selected.)
All the lines that start with 4 spaces'
Lines that bedin with "<p" or "<d"
Lines that only contain HTML closing tags( for example, "</div>")
Open the StringPractice02 project and make the followng changes. Please note that the
code to read a file has been supplied for you.
1. Edit the FindText.java file.
2. Create fields for a Pattern and a Matcher object
private Pattern pattern;
private Matcher m;
3. Outside the search loop, create and initialize your pattern object.
pattern = pattern.compile("<h4>");
4. Inside the search loop, generate a Matcher based on the supplied Pattern
object.
m = pattern.matcher(line);
5. Inside the search loop, search each line for the pattern supplied. print the line
number and the line that has matching text.
if (m.find() ) {
System.out.println(" " + " "+ line);
}
6. Run the FindText.java file and search for these patterns.
All the lines that contain <h4>
pattern = Pattern.compile("<h4>");
All the lines that contain the word "to" (For example, line 17 should
not be selected.)
pattern = Pattern.compile("\\bto\\b"0;
All the lines that start with 4 spaces
pattern = Pattern.compile("^\\s{4}");
Lines that begin with "<p" or "<d"
pattern = Pattern.compile("^<[p|d]");
Lines that only contain HTML closin tags(For example, "</div>")
pattern = Pattern.compile("^</.*?>$");
SOLUTION
Create a simple applications that will loop through a text file(gettys.html) and search for
text by using regular expresions. If the desired text found on a line, print out the line number
and the line text.
9 <h4>Abrahan Lincoln</h4>
10 <h4>Thrusday, November 19, 1863</h4>
Tasks
The gettys.html file is located in the root of the project folder. To examine the file,
with the project open, click the file tab. Double-click the file to open it and exmaine its
content.
1. Edit teh FindText.java file.
2. Create a pattern and a Matcher field.
3. Generate a Matcher based on the supplied Pattern object.
4. Search each line for the pattern supplied.
5. Print the line numver and the line that has matching text.
6. Run the FindText.java file and search for these patterns.
All lines that contain <h4>
All the ines that contain the word "to" (For example, line 17 should
not be selected.)
All the lines that start with 4 spaces'
Lines that bedin with "<p" or "<d"
Lines that only contain HTML closing tags( for example, "</div>")
Open the StringPractice02 project and make the followng changes. Please note that the
code to read a file has been supplied for you.
1. Edit the FindText.java file.
2. Create fields for a Pattern and a Matcher object
private Pattern pattern;
private Matcher m;
3. Outside the search loop, create and initialize your pattern object.
pattern = pattern.compile("<h4>");
4. Inside the search loop, generate a Matcher based on the supplied Pattern
object.
m = pattern.matcher(line);
5. Inside the search loop, search each line for the pattern supplied. print the line
number and the line that has matching text.
if (m.find() ) {
System.out.println(" " + " "+ line);
}
6. Run the FindText.java file and search for these patterns.
All the lines that contain <h4>
pattern = Pattern.compile("<h4>");
All the lines that contain the word "to" (For example, line 17 should
not be selected.)
pattern = Pattern.compile("\\bto\\b"0;
All the lines that start with 4 spaces
pattern = Pattern.compile("^\\s{4}");
Lines that begin with "<p" or "<d"
pattern = Pattern.compile("^<[p|d]");
Lines that only contain HTML closin tags(For example, "</div>")
pattern = Pattern.compile("^</.*?>$");
No comments:
Post a Comment