import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^(basedir[ ]*=[ ]*\")(.)(:[\\/\\\\])([a-zA-Z0-9]*)([\\/\\\\]?.*)$";
final String string = "[mysqld]\n"
+ "#skip-innodb\n\n"
+ "# The TCP/IP Port the MySQL Server will listen on\n"
+ "port=3306\n"
+ "max_allowed_packet=16M\n\n\n"
+ "#Path to installation directory. All paths are usually resolved relative to this.\n"
+ "#basedir=\"C:/Program Files/MySQL/MySQL Server 5.1/\"\n"
+ "basedir=\"C:/MySQL/\"\n\n"
+ "#Path to the database root\n"
+ "#datadir=\"C:/Documents and Settings/All Users/Application Data/MySQL/MySQL Server 5.1/Data/\"\n"
+ "datadir=\"C:/MySQLData/\"\n\n"
+ "# The default character set that will be used when a new schema or table is\n"
+ "# created and no character set is defined\n"
+ "default-character-set=utf8";
final String subst = "$1Z$3Here$5";
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