Java Programing laungage

java.util Projects

java.util Project 1

Parsing Character-Separated Data with a Regular Expression

In this programming tutorials illustrates you how to split String data (separated by "," or "and/or"). These words are shown in a new line without the word or characters which is used for separation the word of the sentences through the given regular expression of the following program.

Previous Home Next

Program Description

In this example we are going to take a complete string or text with the many separation and detects every comma, and, or separation from the given sentences. It besides all word or the collection of words from where the comma, and, or separation is used.

Code Description

String[] str = string.split("[, ]+(and|or)*[ ]")

This code is used to split the value of String type variable string with the ","(comma), " ", "and" or "or" separators and store it into a String array str1 which value is shown by the following program one-by-one.

Example

package r4r;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class seperatedatatest {
public static void main(String[] args) throws IOException{
 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 System.out.print("Enter string for finding separated data: ");
 String str = br.readLine();
 String[] str1 = str.split("[, ]+(and|or)*[ ]");
 for (int i = 0; i < str1.length; i++){
 System.out.println(str1[i]);
 }
 }
}
Previous Home Next