有一个预先存在的正则表达式,在一种情况下应该允许多一个字符:
String regex = "^[a-zA-Z0-9\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF \\-&*()_+|~=`{}\\[\\]:\";'<>?,.]{0,900}$";
if (condition is true) {
regex = regex .substring(0, 47) + "/" + regex.substring(47,
regex.length());
}
最佳答案
最好的方法可能是单独定义它们,然后根据您的 if 条件使用它们。
String regex1 = "^[a-zA-Z0-9\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF \\-&*()_+|~=`{}\\[\\]:\";'<>?,.]{0,900}$";
String regex2 = "^[a-zA-Z0-9\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF \\-&*()_+|~=`{}\\[\\]:\";'<>?,/.]{0,900}$";
String currentRegex;
if (condition is true) {
currentRegex = regex2;
} else {
currentRegex = regex1;
}
关于java - 修改java现有正则表达式以允许一个新字符的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52832583/