Match single word by regex - Java Regular Expressions

Java examples for Regular Expressions:Word

Description

Match single word by regex

Demo Code




import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main 
{
  public static void main(String args[])throws IOException
  {/*from   ww w .  j  ava  2  s . c om*/
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    int no = Integer.parseInt(br.readLine());
    
    while(no>0)
    {
      String str = br.readLine();
      String pat="\\b(C|CPP|JAVA|PYTHON)\\b";
      Pattern p = Pattern.compile(pat);
      Matcher m = p.matcher(str);
      
      if(m.find())
      {
        System.out.println("VALID");
      }
      else
      {
        System.out.println("INVALID");
      }
      no--;
    }
  }
}

Related Tutorials