Finding a substring within a string using regular expressions

I'm new to Java and is trying to find a substring within a string using regular Expressions. I was just wondering if there are any alternative ways to achieve the same goal, or in a more elegant or efficient manner.

while (true) {

Pattern pattern

System.out.println("\nEnter input string to search:");
Matcher matcher

boolean found = false;
while (matcher.find()) {
System.out.format("I found the text"
+ " \"%s\" starting at "
+ "index %d and ending at index %d.%n",
matcher.group(),
matcher.start(),
matcher.end());
found = true;
}
if (!found) {
System.out.println("No match found.");
}
}

• It is not clear to me what you want to do here as your code allows for all kinds of regex being entered. What is the main goal and requirements? Cos you also give some examples of input and expected output? – holroy Mar 3 '16 at 13:51

You code is pretty much good but

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));


may be easily replaced with

Console br = System.console();


Also your while cycle is endless. I would allow to enter some keyword for breaking the loop. E.g. exit

...