Share on Google+Share on Google+

Find Successively Repeated Character using Java

Advertisement
In this section, we are going to find the successively repeated character using Regular Expression.

Find Successively Repeated Character using Java

In this section, we are going to find the successively repeated character using Regular Expression. For this, we have specified a word 'programming' and with use of Pattern and Matcher class, we have found that 'm' is repeated successively.

Here is the code of Main_Class.java

import java.util.regex.*;

public class Main_Class{
public static void main(String[] args){
String st = "programming";
Pattern p = Pattern.compile("(\\w)\\1+");
Matcher m = p.matcher(st);
if (m.find()){
System.out.println("Repeated character is " + m.group(1));
}
}
}

Output:

Repeated Character is m

Advertisements

Advertisement

Posted on: October 24, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.