Java 正则表达式查找带方括号的字符串并替换
我现在的代码是Java 正则表达式查找带方括号的字符串并替换,java,regex,replace,Java,Regex,Replace,我现在的代码是 ` String text= "[School_Teacher_Name] is our new member, So please congratulate [School_Teacher_Name] ."; String tag = "[School_Teacher_Name]"; String value= "Yash Mathur"; String str1 = tag.substring(1, tag.length()-1); String reg = "/\\["+s
`
String text= "[School_Teacher_Name] is our new member, So please congratulate [School_Teacher_Name] .";
String tag = "[School_Teacher_Name]";
String value= "Yash Mathur";
String str1 = tag.substring(1, tag.length()-1);
String reg = "/\\["+str1+"\\]/";
if(text.contains(tag)){
return text.replaceAll(reg, value).trim();
}
else{
return text;
}`
我在正则表达式方面没有太多经验。我的代码没有替换任何值,请帮助我。从正则表达式中删除正斜杠 改变
String reg = "/\\["+str1+"\\]/";
到
输出
Yash Mathur is our new member, So please congratulate Yash Mathur .
PS-在这种情况下,最好使用
text.replace(tag,value)
而不是text.replaceAll(reg,value)
从正则表达式中删除前斜杠
改变
String reg = "/\\["+str1+"\\]/";
到
输出
Yash Mathur is our new member, So please congratulate Yash Mathur .
PS-在这种情况下,最好使用
text.replace(tag,value)
而不是text.replaceAll(reg,value)
如果我没有误解您的需求,那么您可以这样做
Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
final String regex = "\\[(School_Teacher_Name)]";
final String string = "[School_Teacher_Name] is our new member, So please congratulate [School_Teacher_Name] .";
final String subst = "Yash Mathur";
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);
Javascript
Yash Mathur is our new member, So please congratulate Yash Mathur .
const regex=/\[(学校教师姓名)]/gm;
const str=`[学校教师姓名]是我们的新成员,请祝贺[学校教师姓名];
const subst=`Yash Mathur`;
//被替换的值将包含在结果变量中
const result=str.replace(regex,subst);
log('替换结果:',结果)代码>如果我没有误解您的需求,那么您可以这样做
Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
final String regex = "\\[(School_Teacher_Name)]";
final String string = "[School_Teacher_Name] is our new member, So please congratulate [School_Teacher_Name] .";
final String subst = "Yash Mathur";
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);
Javascript
Yash Mathur is our new member, So please congratulate Yash Mathur .
const regex=/\[(学校教师姓名)]/gm;
const str=`[学校教师姓名]是我们的新成员,请祝贺[学校教师姓名];
const subst=`Yash Mathur`;
//被替换的值将包含在结果变量中
const result=str.replace(regex,subst);
log('替换结果:',结果)代码>使用它要容易得多
let teacher='Yash Mathur';
让test=`${teacher}是我们的新成员,所以请祝贺${teacher}`;
控制台日志(测试)代码>使用它要容易得多
let teacher='Yash Mathur';
让test=`${teacher}是我们的新成员,所以请祝贺${teacher}`;
控制台日志(测试)代码>替换为什么?相同的字符串?这不是JavaScript。这是Java。这两种编程语言完全不同。替换为什么?相同的字符串?这不是JavaScript。这是Java。这两种是完全不同的编程语言。那么,我猜使用:)那么,我猜使用:)谢谢allot,它起作用了,我不需要模式和匹配器,我的字符串类方法replaceAll需要正则表达式。@SurendraKumarJaiswal不客气,很高兴它能帮到你。我可以问一下为什么在结尾“]”,我们没有使用“/]”吗?谢谢allot,它起作用了,我不需要pattern和matcher,我的字符串类方法replaceAll接受regex。@SurendraKumarJaiswal不客气,很高兴它能帮上忙。我可以问一下为什么在“]”结尾处,我们没有使用“/”?