Write a java program to check regular expression.

Given a string (s) apply the following rules.
1. The string should not begin with a number.
If the condition is satisfied then print TRUE else print FALSE.
Input and Output Format:
Input consists of a string.
The output consists of TRUE or FALSE.
Refer sample output for formatting specifications.

Sample Input 1:
ab2
Sample Output 1:
TRUE

Sample Input 2:
72CAB
Sample Output 2:
FALSE

Check regular expression in Java.

import java.util.*;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    String s = sc.nextLine();
    boolean b = matchCharacter(s);
    if (b == true) {
      System.out.println("TRUE");
    } else {
      System.out.println("FALSE");
    }
  }

  public static boolean matchCharacter(String s) {
    boolean b = false;
    if (s.matches("[0]{2}[0-9]{8}")) {
      b = false;
    } else if (s.matches("[0-9]{10}")) {
      b = true;
    }
    return b;

  }
}

Given a string (s) apply the following rules.

1. The string consists of three characters only.
2. The characters should be alphabets only.

If all the conditions are satisfied then print TRUE else print FALSE.

Input and Output Format:

Input consists of a string.
Output consists of TRUE or FALSE .
Refer sample output for formatting specifications.

Sample Input 1:
AcB

Sample Output 1:
TRUE

Sample Input 2:
A2B

Sample Output 2:
FALSE

import java.util.Scanner;

public class Main {
  public static void main(String args[]) {
    Scanner sc = new Scanner(System.in);
    String s = sc.next();
    boolean b1 = validString(s);
    System.out.println(b1);
  }

  public static boolean validString(String s) {
    boolean b = false;
    StringBuffer sb = new StringBuffer();
    if (s.length() == 3) {
      if (s.matches("[a-zA-Z]{3}")) {
        b = true;
      }

      else
        b = false;

    }

    else
      b = false;
    return b;
  }
}

Add a Comment

Your email address will not be published. Required fields are marked *

16 + four =