import java.util.regex.Matcher; import java.util.regex.Pattern; // Class definition for WordOccurrencesExample public class WordOccurrencesExample { // Main method public static void main(String[] args) { // Create a sample text String text = "Java is a versatile programming language. Java is widely used in software development."; // Define the word to find occurrences String wordToFind = "Java"; // Create a regex pattern using the word Pattern pattern = Pattern.compile("\\b" + wordToFind + "\\b", Pattern.CASE_INSENSITIVE); // Create a matcher for the text Matcher matcher = pattern.matcher(text); // Find and display every occurrence of the word System.out.println("Occurrences of the word '" + wordToFind + "':"); while (matcher.find()) { System.out.println("Found at index " + matcher.start() + " - " + matcher.group()); } } }