split based on regex in java code example

Example 1: Regex split java

package com.tutorialspoint;

import java.lang.*;

public class StringDemo {

   public static void main(String[] args) {

      String str = "a d, m, i.n";
      String delimiters = "\\s+|,\\s*|\\.\\s*";

      // analyzing the string 
      String[] tokensVal = str.split(delimiters);

      // prints the number of tokens
      System.out.println("Count of tokens = " + tokensVal.length);
    
      for(String token : tokensVal) {
         System.out.print(token);
      } 
   }
}

Example 2: Splitting a string with regex in java

String current_url = "https://{domain name}/{type of data}/4583236-{name-of-perpetrators}";
    String[] urlParts = current_url.split("type of data}/");
    String mySuburl = urlParts[1];
    String[] suburl = mySuburl.split("-{name-of-perpetrators");
    String mytext = suburl[0];
    System.out.println(mytext);

Tags:

Java Example