Let’s say we have a text document containing multiple paragraphs, and we want to extract paragraphs that start with a capital letter and end with a full stop. We can achieve this using regular expressions. Here’s an example code snippet:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ParagraphMatcher {
public static void main(String[] args) {
String text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.\n\n" +
"Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, " +
"totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt " +
"explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, " +
"sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt.\n\n" +
"Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, " +
"nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate " +
"velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur.";
Pattern pattern = Pattern.compile("^([A-Z].*?\\.)$", Pattern.MULTILINE | Pattern.DOTALL);
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}
In this example, we use the Pattern
class to compile a regular expression that matches paragraphs starting with a capital letter and ending with a full stop. The regular expression ^([A-Z].*?\\.)$
breaks down as follows:
^
: Matches the beginning of a line.[A-Z]
: Matches a single capital letter..*?
: Matches any character (except newline) zero or more times, non-greedily.\\.
: Matches a full stop.$
: Matches the end of a line.
We enable Pattern.MULTILINE
to treat each line as a separate string and Pattern.DOTALL
to allow the dot (.) character to match newline characters.
The Matcher
class then applies the regular expression to the input text
and finds all matches. We iterate over the matches using the find()
method and print each matching paragraph using the group()
method.
By using regular expressions, we can easily match and extract specific paragraph patterns within a larger text document. This provides a powerful mechanism for manipulating and analyzing text data in Java applications.
#Java #RegularExpressions