Home Regularexpressions Split string after each character using regular expression



Split string after each character using regular expression
Posted on: August 30, 2008 at 12:00 AM
This Example describes the way to split the String after each character using expression. For this we are going to make program named SplittingFind. java.

Split string after each character using regular expression

     

This Example describes the way to split the String after each character using expression. For this we are going to make program named SplittingFind. java. The steps involved in program SplittingFind.java are described below:

String regex = ("\\w.+"):- Creating expression for text. Here "\\w" means to match any alphanumeric character.

Pattern p = Pattern.compile(regex):- Creating a pattern object and compiles the given regular expression into a pattern.

Matcher m = p.matcher(text):- Creates a matcher for matching the given input against pattern p.

m.find():- This is the method of matcher class which is used for finding the next subsequence of the input sequence and it usually returns a boolean value.

m.group():- It returns the input sequence match with the first match.

SplittingFind.java

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

public class SplittingFind {

  public static void main(String[] args) {
  String regex = ("\\w.+");
  Pattern p = Pattern.compile(regex);
  String text = "Roseindia.net Rohini";
  Matcher m = p.matcher(text);
  System.out.print("Text is:");
  while (m.find()) {
  System.out.print(m.group());
  }
  System.out.println();
  int i = 0;
  while (m.find(i)) {
  System.out.print(m.group() + "\n");
  i++;
  }
  }
}

Output of the program:-

Text is:Roseindia.net Rohini
======================
Roseindia.net Rohini
oseindia.net Rohini
seindia.net Rohini
eindia.net Rohini
india.net Rohini
ndia.net Rohini
dia.net Rohini
ia.net Rohini
a.net Rohini
net Rohini
net Rohini
et Rohini
t Rohini
Rohini
Rohini
ohini
hini
ini
ni

Download Source Code

Related Tags for Split string after each character using regular expression:
javacstringiomakefindsplitcharexpressionnameusingthischaracterforexamplecteprogramtoexpressramexameachsplittingssieitdeslisplinmtrjafteresnamedmeprodescribexaxampsspessrackishamplpresspregoeaaractstrxpvattscrssriringthavbesstaffinpleplprndonogro


More Tutorials from this section

Ask Questions?    Discuss: Split string after each character using regular expression  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.