import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = ".+?snuffs out the (\\d+).. fire pit, and in seconds.+";
final String string = "\\#58FAACQaaron snuffs out the 1st fire pit, and in seconds the connecting bridge will also be extinguished!\n\n"
+ "\\#58FAACQaaron snuffs out the 2nd fire pit, and in seconds the connecting bridge will also be extinguished!\n\n"
+ "\\#58FAACQaaron snuffs out the 3rd fire pit, and in seconds the connecting bridge will also be extinguished!\n\n"
+ "\\#58FAACQaaron snuffs out the 4th fire pit, and in seconds the connecting bridge will also be extinguished!\n\n"
+ "\\#58FAACQaaron snuffs out the 5th fire pit, and in seconds the connecting bridge will also be extinguished!\n\n"
+ "\\#58FAACQaaron snuffs out the 6th fire pit, and in seconds the connecting bridge will also be extinguished!\n\n"
+ "\\#58FAACQaaron snuffs out the 7th fire pit, and in seconds the connecting bridge will also be extinguished!\n\n"
+ "\\#58FAACQaaron snuffs out the 8th fire pit, and in seconds the connecting bridge will also be extinguished!\n\n"
+ "\\#58FAACQaaron snuffs out the 9th fire pit, and in seconds the connecting bridge will also be extinguished!\n\n"
+ "\\#58FAACQaaron snuffs out the 10th fire pit, and in seconds the connecting bridge will also be extinguished!";
final String subst = "Bridge $1";
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(string);
// The substituted value will be contained in the result variable
final String result = matcher.replaceAll(subst);
System.out.println("Substitution result: " + result);
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html