This Example describes the way for finding start and end index of the string using regular expression. For this we are going to make program named Finding_index.java. The steps involved in program Finding_index.java are described below:-
String[] text = {"Russian military convoy passed through Russian" + " Mountains in Russian Ossetia"}:-Declaring text from where the index of the particular word "Russian" will be retrieve.
String regex="R\\w*":-This is the expression to search words starting with letter "R".
Pattern p = Pattern.compile(regex):-Creating a pattern object and compiling the given regular expression into a pattern.
(matcher.find()):-This is the method of matcher class which is generally used for finding the next subsequence of the input sequence, it usually returns a boolean value.
matcher.start():-This method is used for finding the start index of the word starting with letter R.
matcher.end():-This method is used for finding the end index of the word starting with letter R.
Finding_index.java
|
Output of the program:-
|
Text is : Russian military convoy passed through Russian Mountains in Russian Ossetia ======================== Starting & ending index ofRussian:=start=0 end = 7 Starting & ending index ofRussian:=start=39 end = 46 Starting & ending index ofRussian:=start=60 end = 67 ======================== |
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.
Ask Questions? Discuss: Finding start and end index of string using Regular expression
Post your Comment