Java Regex to Match Misspell Words

In this regex tutorial, we give an example of matching all common misspellings of any word in text content. An example could be of word “calendar”. Common misspellings of ‘calendar’ may include incorrect use of vowels at positions 2, 4, and 7. In this example, I will check for the wrong characters at these positions only. we can modify the example code as per your need.

1. Regex to match ‘calendar’ with misspellings

The following regex will be able to match the word ‘calendar’ and all of its common misspellings.

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

2. Demo

The following example demonstrates the usage of regex to find all misspellings.

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());
}

The program 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 modifying the above example code and playing with it for more possible misspellings you can think of, and then try matching them.

Happy Learning !!

Sourcecode on Github

Comments

Subscribe
Notify of
guest
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

Dark Mode

Dark Mode