![]() |
Java’s regular expressions, or regex, let you do advanced text manipulation and matching. Regex offers a handy approach for searching for a term in a text wherever it appears. In this article, we will learn to find every occurrence of a word using regex. Program to Find Occurrence of a Word Using RegexThe primary idea is to use Java’s java.util.regex library, namely the Pattern and Matcher classes. You may create a pattern that matches a certain word or character sequence using regular expressions. The Matcher class assists in locating instances of the pattern within a supplied text, while the Pattern class assembles the regex pattern. Below is the implementation of finding the occurrence of a word using Regex:Java
Output
Occurrences of the word 'Java': Found at index 0 - Java Found at index 42 - Java Explaination of the above Program:
|
Reffered: https://www.geeksforgeeks.org
Java |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 11 |