Last Index of a Word
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main
{
public static void main(String args[])
{
String candidateString = "This is a Java example.
This is another Java example.";
Pattern p = Pattern.compile("Java");
Matcher matcher = p.matcher(candidateString);
matcher.find();
int nextIndex = matcher.end();
System.out.print("The last index of Java is:");
System.out.println(nextIndex);
}
}
Pattern matching
import java.util.regex.*;
import java.io.*;
public class ReaderIter
{
public static void main(String[] args) throws IOException
{
Pattern patt = Pattern.compile("[A-Za-z][a-z]+");
BufferedReader r = new BufferedReader
(new FileReader("ReaderIter.java"));
String line;
while ((line = r.readLine()) != null)
{
Matcher m = patt.matcher(line);
while (m.find())
{
System.out.println(m.group(0));
int start = m.start(0);
int end = m.end(0);
Use CharacterIterator.substring(offset, end);
System.out.println(line.substring(start, end));
}
}
}
}
No comments:
Post a Comment