import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "([^ ]+ ){0,4}(?:qyw4)( [^ ]+){0,5}";
final String string = "12 That led them by the right hand of Moses with his glorious arm, dividing the water before them, to qyw4 make himself an everlasting name? 13 That led them through the deep, as an horse in the wilderness, that they should not stumble? 14 As a beast goeth down into the valley, the Spirit of the LORD caused him to rest: so didst thou lead thy people, to make thyself a glorious name. \n\n\n"
+ "15 Look down from heaven, and behold from the qyw4 habitation of thy holiness and of thy glory: where is thy zeal and thy strength, the sounding of thy bowels and of thy mercies toward me? are they restrained? ";
final String subst = "love $1$2 unny\\r\\r";
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