import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^(\\/.*)\\n(.+)\\n*|(?<=^(\\/.*)\\n(?:.+\\n)*)(.+)\\n*";
final String string = "こんにちは\n"
+ "質問です\n\n"
+ "●Regular Expressionの使用環境\n"
+ "普通のテキストファイル\n"
+ "いま Sublime text を使ってますが無料のエディタなら何でも使います\n\n"
+ "●検索か置換か?\n"
+ "置換\n\n"
+ "●説明\n"
+ "各パラグラフの1行目を2行目以下の全ての行に加えたい\n"
+ "1行目はパラグラフごとに異なり、2行目以下はすべて異なる\n\n"
+ "●対象データ\n"
+ "/system/app\n"
+ "AntHalService\n"
+ "AutoRegistration\n"
+ "BasicDreams\n\n"
+ "/system/priv-app\n"
+ "AutoKillService\n"
+ "BackupRestoreConfirmation\n\n"
+ "●希望する結果\n"
+ "AntHalService^=/system/app/AntHalService\n"
+ "AutoRegistration^=/system/app/AutoRegistration\n"
+ "BasicDreams^=/system/app/BasicDreams\n"
+ "AutoKillService^=/system/priv-app/AutoKillService\n"
+ "BackupRestoreConfirmation^=/system/priv-app/BackupRestoreConfirmation\n\n"
+ "よろしくお願いします";
final String subst = "$2$4^=$1$3/$2$4\\n";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
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