- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Match all occurrences of a regex in Java
public class RegexOccur { public static void main(String args[]) { String str = "java is fun so learn java"; String findStr = "java"; int lastIndex = 0; int count = 0; while(lastIndex != -1) { lastIndex = str.indexOf(findStr,lastIndex); if(lastIndex != -1) { count ++; lastIndex += findStr.length(); } } System.out.println(count); } }
Output
2
- Related Articles
- How to match n number of occurrences of an expression using Java RegEx?
- How to match a range of characters using Java regex
- How match a string irrespective of case using Java regex.
- Java regex program to match parenthesis "(" or, ")".
- How to match a fixed set of characters using Java RegEx
- Regex to match lines containing multiple strings in Java
- How to match a line not containing a word in Java Regex
- Significance of regex match() and regex search() function in Python
- How to match a non-word character using Java RegEx?
- How to match a white space equivalent using Java RegEx?
- How to match end of a particular string/line using Java RegEx
- How to match beginning of a particular string/line using Java RegEx
- How to match any character using Java RegEx
- How to match word characters using Java RegEx?
- How to match word boundaries using Java RegEx?

Advertisements