Parsing Character-Separated Data with a Regular Expression

Ads
 

Parsing Character-Separated Data with a Regular Expression

This section 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 fol

This section 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 fol

Parsing Character-Separated Data with a Regular Expression

     

This section 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.

Program Result:

This program takes a complete string or text with the many separation of words and detects every comma, and|or separation from the given sentences. It besides all word or the collection of words from where the comma or and|or separation is used. The complete separate word is shown in a new line.

Code Description:

String[] str = string.split("[, ]+(and|or)*[ ]"):
Above code splits the value of String type variable string with the ","(comma), " ", "and" or "or" separators and store it into a String array str which value is shown by the following program one-by-one.

Here is the code of the program:

import java.util.regex.*;
import java.io.*;

public class ParseSeparatedData{
  public static void main(String[] argsthrows IOException{
  BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  System.out.print("Enter string for finding separated data: ");
  String string = in.readLine();
  String[] str = string.split("[, ]+(and|or)*[ ]");
  for (int i = 0; i < str.length; i++){
  System.out.println(str[i]);
  }
  }
}

Download this example.

Ads

Ads