Sunday 13 December 2015

Java Lab@home 8

1.Write a program to accept a three letter word as an input from the user and display the message, Expression Matched, if the word starts with the letter, "a", "b", or "c", and ends with, "at".

Ans. import java . util . Scanner;
import java . util . regex . Matcher;
imort java  . util . regex . Pattern;

 public class TestRegx {

 public static void main (String [] args) {
 Scanner sc = new Scanner (System . in);
 String input;

 System.out..print ("Enter the string: ");
 input = sc . next ();
 Pattern myPattern = Pattern . compile (" [abc] at");
 Matcher myMatcher = myPattern . matcher (input);
 boolen  myBoolen  = myMatcher . matches ();
 if (myBoolen) {
      System.out.println ("Expression is Matched");
    } else {
      System.out.println ("Expression Not Matched");
    }
  }

}



2.• • • Write a program that accepts the URL of the Web portal from the user, such as http://www.xyz.com, www.xyz.com, or ftp://xyz.com, and validate the format of the URL with the following fields: Protocol, such as https or http Domain, such as google or yahoo Top level domain, such as .co or .com

Ans import java . util . Scanner;
import java . util . regex . Matcher;
import java . util . regex . Pattern;

class UrlChecker {

 public static void main (String [] args) {
   pattern Mypattern = Pattern . compile ("^ ( (https ? |
 ftp) : // | (www | ftp) \\ .) [ a-zO-9-] + [a-z] + ) + ( [/ ?] . *) ?$" );
      Scanner input = new Scanner (System . in);
      System.out.println ("Enter the Url  to be checked:
  ");
      String name = input . nextLine ();
      Matcher Mymatcher = Mypattern . matcher (name);
      Boolen Myboolen = Mymatcher . matches ();
      if (Myboolen == true) {
          System.out.println ("Url is correct");
       } else {
          System.out.println ("Url is incorrect");
     }
  }

}

3.Write a program to accept the date from the user and validate it according to the format, DD/MM/YYYY or D/M/YYYY. In addition, the year should be from 1900 to 2099.

Ans CLICK HERE


4.Parse comma-delimited text and convert the data into Shirt objects.

Ans

5.Create a program that searches a text file by using regular expressions.

Ans 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>")

        

6.Use regular expressions to transform<p>tags <span>into tags.

Ans CLICK HERE

No comments:

Post a Comment