Tutorialspoint

Name validation using Java Regular Expressions

public class Demo {
   public static void main(String args[]) {
      String name = "John Harry Smith";

      String regexName = "\\p{Upper}(\\p{Lower}+\\s?)";
      String patternName = "(" + regexName + "){2,3}";

      System.out.println("The name is: " + name);
      System.out.println("Is the above name valid? " + name.matches(patternName));

   }
}

Split a string around a particular match in Java

public class Demo {
   public static void main(String args[]) {
      String regex = "_";
      String strInput = "The_sky_is_blue";
      System.out.println("Regex: " + regex);
      System.out.println("Input string: " + strInput);
      String[] strArr = strInput.split(regex);
      System.out.println("\nThe split input string is:");
      for (String s : strArr) {
         System.out.println(s);
      }
   }
}

Demonstrate the usage of the Pattern.split() method in Java

import java.util.regex.Pattern;

public class Demo {
   public static void main(String[] args) {
      String regex = "_";
      String input = "Oranges_are_orange";
      System.out.println("Regex: " + regex);
      System.out.println("Input: " + input);
      Pattern p = Pattern.compile(regex);
      String[] str = p.split(input);
      System.out.println("\nThe split input is:");
      for (String s : str) {
         System.out.println(s);
      }
   }
}

Validate Email address in Java

public class Demo {
   static boolean isValid(String email) {
      String regex = "^[\\w-_\\.+]*[\\w-_\\.]\\@([\\w]+\\.)+[\\w]+[\\w]$";
      return email.matches(regex);
   }

   public static void main(String[] args) {
      String email = "john123@gmail.com";
      System.out.println("The E-mail ID is: " + email);
      System.out.println("Is the above E-mail ID valid? " + isValid(email));
   }
}

Pattern.matches() method in Java Regular Expressions

import java.util.regex.Pattern;

public class Demo {
   public static void main(String args[]) {
      String regex = "a*b";
      String input = "aaab";
      System.out.println("Regex: " + regex);
      System.out.println("Input: " + input);
      boolean match = Pattern.matches(regex, input);
      System.out.println("\nDoes the regex match with the input? " + match);
   }
}

Reset the Matcher in Java Regular Expressions

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

public class MainClass {
   public static void main(String args[]) {
      Pattern p = Pattern.compile("(a*b)");
      Matcher m = p.matcher("caaabcccab");
      System.out.println("The input string is: caaabcccab");
      System.out.println("The Regex is: (a*b)");
      System.out.println();
      while (m.find()) {
         System.out.println(m.group());
      }
      m.reset();
      System.out.println("\nThe Matcher is reset");
      while (m.find()) {
         System.out.println(m.group());
      }
   }
}

subsequence in an input sequence that matches the pattern required in Java Regular Expressions

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

public class Demo {
   public static void main(String args[]) {
      Pattern p = Pattern.compile("Sun");
      Matcher m = p.matcher("The Earth revolves around the Sun");
      System.out.println("Subsequence: Sun");
      System.out.println("Sequence: The Earth revolves around the Sun");
      if (m.find())
         System.out.println("\nSubsequence found");
      else
         System.out.println("\nSubsequence not found");
   }
}

Working with Matcher.end() method in Java Regular Expressions

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

public class Demo {
   public static void main(String args[]) {
      Pattern p = Pattern.compile("(a*b)");
      Matcher m = p.matcher("caaabccaab");
      System.out.println("The input string is: caaabccaab");
      System.out.println("The Regex is: (a*b)");
      System.out.println();
      while (m.find()) {
         System.out.println("Index: " + m.end());
      }
   }
}

Working with Matcher.end() method in Java Regular Expressions

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

public class Demo {
   public static void main(String args[]) {
      Pattern p = Pattern.compile("(a*b)");
      Matcher m = p.matcher("caaabccaab");
      System.out.println("The input string is: caaabccaab");
      System.out.println("The Regex is: (a*b)");
      System.out.println();
      while (m.find()) {
         System.out.println("Index: " + m.end());
      }
   }
}

Use a character class in Java Regular Expressions

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

public class Demo {
   public static void main(String args[]) {
      Pattern p = Pattern.compile("[a-z]+");
      Matcher m = p.matcher("the sky is blue");
      System.out.println("The input string is: the sky is blue");
      System.out.println("The Regex is: [a-z]+");
      System.out.println();
      while (m.find()) {
         System.out.println("Match: " + m.group());
      }
   }
}

1 2 3 4 5 6 7 ... 444 Next
Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.