regex

Find duplication in strings example

With this example we are going to demonstrate how to find duplication in Strings. In order to check if there are duplications in a String we have created a Matcher against a specified Pattern and used the Matcher API in boolean hasDuplicateWord(String phrase) method. In short the steps of the method are:

  • Compile a String regular expression to a Pattern, using compile(String regex) API method of Pattern. The regular expression used is a word boundary followed by a word character one or more times, followed by the digit 1 and then again a word boundary.
  • Use matcher(CharSequence input) API method of Pattern to create a Matcher that will match the given String input against this pattern.
  • While the matcher finds the next subsequence of the input sequence that matches the pattern, with find() API method of Matcher get the input subsequence matched, with group() API method of Matcher and print it.

Let’s take a look at the code snippet that follows:

package com.javacodegeeks.snippets.core;

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

public class FindDup {

    public static void main(String args[]) {


  hasDuplicateWord("pizza pizza");

  hasDuplicateWord("Faster pussycat kill kill");

  hasDuplicateWord("The mayor of of simpleton");

  hasDuplicateWord("Never Never Never Never Never");

  hasDuplicateWord("222 2222");

  hasDuplicateWord("sara sarah");

  hasDuplicateWord("Faster pussycat kill, kill");

  hasDuplicateWord(". .");
    }

    public static boolean hasDuplicateWord(String phrase) {

  boolean retval = false;

  String duplicatePattern = "\b(\\w+))\1\b";

  Pattern p = null;

  try {


p = Pattern.compile(duplicatePattern);

  } catch (PatternSyntaxException pex) {


pex.printStackTrace();


System.exit(0);

  }

  int matches = 0;

  Matcher m = p.matcher(phrase);

  String val = null;


  while (m.find()) {


retval = true;


val = ":" + m.group() + ":";


System.out.println(val);


matches++;

  }


  String msg = "   NO MATCH: pattern:" + phrase


    + "rn

 regex: " + duplicatePattern;


  if (retval) {


msg = " MATCH     : pattern:" + phrase + "rn
   regex: "



  + duplicatePattern;

  }


  System.out.println(msg + "rn");

  return retval;
    }
}

Output:

:pizza pizza:
 MATCH     : pattern:pizza pizza

   regex: b(w+) 1b

:kill kill:
 MATCH     : pattern:Faster pussycat kill kill

   regex: b(w+) 1b

:of of:
 MATCH     : pattern:The mayor of of simpleton

   regex: b(w+) 1b

:Never Never:
:Never Never:
 MATCH     : pattern:Never Never Never Never Never

   regex: b(w+) 1b

   NO MATCH: pattern:222 2222


 regex: b(w+) 1b

   NO MATCH: pattern:sara sarah


 regex: b(w+) 1b

   NO MATCH: pattern:Faster pussycat kill, kill


 regex: b(w+) 1b

   NO MATCH: pattern:. .


 regex: b(w+) 1b

 
This was an example of how to find duplication in Strings in Java.

Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button