Java Regex – Match Word with All Misspellings

In this tutorial, I am giving a short example of matching all common misspellings along with any word in a text content. An example could be of word “calendar”. Common misspellings of calendar will possibly include incorrect use of vowels at position 2, 4, and 7. In this example, I will check for wrong characters at these positions only. You can modify the example code as per your need.

Regex to match ‘calendar’ with misspellings

c[ae]l[ae]nd[ae]r

Above regex will be able to match the word calendar as well as all of it’s common misspellings.

Example Usage of Regex

package examples.regex;

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

public class MatchMisspellingsExample
{
   public static void main(String[] args)
   {
      String content = "This is may calandar. This is june calander. This is may calendar.";
      String regex = "c[ae]l[ae]nd[ae]r";
      Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
      Matcher matcher = pattern.matcher(content);
      while (matcher.find())
      {
         System.out.print("Start index: " + matcher.start());
         System.out.print(" End index: " + matcher.end() + " ");
         System.out.println(matcher.group());
      }
   }
}

Output:

Start index: 12 End index: 20 calandar
Start index: 35 End index: 43 calander
Start index: 57 End index: 65 calendar

I will suggest to modify above example code and play with it for more possible misspellings you can think of, and then try matching them.

Happy Learning !!

Leave a Reply

0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial