带replace的Java正则表达式
我需要使用正则表达式用{}内的变量替换字符串 例如:带replace的Java正则表达式,java,regex,Java,Regex,我需要使用正则表达式用{}内的变量替换字符串 例如: "Hi, there are {online@US-server} players in the US server" to "Hi, there are 12 players in the US server" 在字符串中,{}中的变量可以大于1 我需要这个代码来允许用户修改消息。示例“US server”中@like之后{}内的变量是由用户输入的,因此没有检查变量的列表。变量可以是尽可能奇怪的,例如:'{online@test-英国}“
"Hi, there are {online@US-server} players in the US server" to "Hi, there are 12 players in the US server"
在字符串中,{}中的变量可以大于1
我需要这个代码来允许用户修改消息。示例“US server”中@like之后{}内的变量是由用户输入的,因此没有检查变量的列表。变量可以是尽可能奇怪的,例如:'{online@test-英国}“{online@asdtest}"
public static String getReplaced(String d) {
String result = d.split("\\{online@")[0];
for (int i = 1; i < d.split("\\{online@").length; i++) {
String dd = d.split("\\{online@")[i];
Pattern p = Pattern.compile("(.*?)}(.*)");
Matcher m = p.matcher(dd);
if (m.lookingAt()) {
int count = 12;
result += count + m.group(2);
} else {
result += dd;
}
}
return result;
}
公共静态字符串被替换(字符串d){
字符串结果=d.split(“\\{online@”)[0];
对于(int i=1;i
似乎有效
似乎很有效。您不需要正则表达式,也不应该使用它,因为使用用户输入作为正则表达式存在一些潜在的安全风险
public String replaceTag(String tag, String replacement, String template) {
private final String decoratedTag = "{online@" + tag + "}";
return template.replace(decoratedTag, replacement);
}
您可以重复调用此选项:
private String template = getTemplateFromUser();
private String output = template.replace('US-server', usServerCount);
output = output.replace('asdtest', testValue);
return output;
…或者,您可以更复杂地循环一组标记/值对,例如从Map
请注意,
String.replace()
只是简单地替换字符序列,它不是正则表达式替换,这对于解决此问题是一件好事。您不需要正则表达式,也不应该使用它,因为将用户输入作为正则表达式使用会有一些潜在的安全风险
public String replaceTag(String tag, String replacement, String template) {
private final String decoratedTag = "{online@" + tag + "}";
return template.replace(decoratedTag, replacement);
}
您可以重复调用此选项:
private String template = getTemplateFromUser();
private String output = template.replace('US-server', usServerCount);
output = output.replace('asdtest', testValue);
return output;
…或者,您可以更复杂地循环一组标记/值对,例如从Map
请注意,String.replace()
只是简单地替换字符序列,而不是正则表达式替换,这对于这个问题是一件好事。使用\{online@(.+)\}
作为正则表达式,使用匹配器获取第一个捕获组。这将为您提供@之后的部分
Regex是指:
:一般字符“{”\\{
:字符串online@
online@
:至少一个字符(任何人)+
:捕获组(.+)
:literal字符'}\\}
String#replaceAll(stringregex,stringreplacement)
(doc)
例如:
int myIntValue = 12;
String myString = "there are {online@US-server} players";
Pattern p = Pattern.compile("\\{online@(.+)\\}") ;
Matcher m = p.matcher(myString) ;
if (m.find()) { // true if the pattern is found anywhere in your string.
System.out.println("Variable part is : " + m.group(1)); // group 1 is the capturing group
System.out.println(myString.replaceAll("\\{online@.+\\}", String.valueOf(myIntValue)));
}
如果需要在单个字符串中查找多个占位符:
String myString = "there are {online@US-server} players ,and {online@whatever}";
Pattern p = Pattern.compile("\\{online@(.+)\\}") ;
Matcher m = p.matcher(myString) ;
while (m.find()) { // true if the pattern is found anywhere in your string.
System.out.println("Variable part is : " + m.group(1)); // group 1 is the capturing group
System.out.println(myString.replace("{online@"+m.group(1)+"}", String.valueOf(myIntValue)));
}
使用\{online@(+)\}
作为正则表达式,使用匹配器获取第一个捕获组。这将为您提供@之后的部分
Regex是指:
:一般字符“{”\\{
:字符串online@
online@
:至少一个字符(任何人)+
:捕获组(.+)
:literal字符'}\\}
String#replaceAll(stringregex,stringreplacement)
(doc)
例如:
int myIntValue = 12;
String myString = "there are {online@US-server} players";
Pattern p = Pattern.compile("\\{online@(.+)\\}") ;
Matcher m = p.matcher(myString) ;
if (m.find()) { // true if the pattern is found anywhere in your string.
System.out.println("Variable part is : " + m.group(1)); // group 1 is the capturing group
System.out.println(myString.replaceAll("\\{online@.+\\}", String.valueOf(myIntValue)));
}
如果需要在单个字符串中查找多个占位符:
String myString = "there are {online@US-server} players ,and {online@whatever}";
Pattern p = Pattern.compile("\\{online@(.+)\\}") ;
Matcher m = p.matcher(myString) ;
while (m.find()) { // true if the pattern is found anywhere in your string.
System.out.println("Variable part is : " + m.group(1)); // group 1 is the capturing group
System.out.println(myString.replace("{online@"+m.group(1)+"}", String.valueOf(myIntValue)));
}
你尝试了什么?@jhamon我尝试了很多东西,但我对这些东西并不熟悉…最后一次尝试是“{online@(.*)}(.*)”请编辑您的问题以包含您尝试过的代码,即使您认为这是错误的,您可能也不应该使用regex@cricket_007我添加了代码。我尝试了很多次,为我的错误代码感到抱歉。您尝试了什么?@jhamon我尝试了很多事情,但我对这些事情不熟悉……最后一次尝试是“{online@(.*)”}(.*)"请编辑您的问题,将您尝试过的代码包括在内,即使您认为这是错误的,您可能也不应该使用regex@cricket_007我添加了代码。我尝试了很多次,为我的错误代码感到抱歉。我没有带变量的路径列表。每个用户都将他们想要的“US server”或“Test server”放在另一个me中thod,你在服务器中得到玩家计数器。所以@之后是服务器的计数器name@kratess然后我认为你需要回到你的问题,仔细地重写它来解释需求。我没有一个包含变量的路径列表。每个用户输入他们想要的,比如“US server”或“Test server”,然后通过另一种方法,你得到玩家数呃在服务器中。所以@之后是服务器的name@kratess然后我想你需要回到你的问题,仔细地重写它来解释需求。这是我回答的第一句话。你只需要将int转换成字符串。好的,你的代码可以改变所有{}到一个int,但现在我必须知道什么int。服务器中有多少玩家在线?我需要更改所有{},服务器名称中的玩家在@I需要知道{server@和}之间是什么,然后用这个替换你的代码。非常感谢你的时间。你的正则表达式不起作用,所以我用(.*)更改了它奇怪。我试过了,效果很好。可能是你允许@后面的值为空吗?这是我回答的第一句话。你只需要将int转换为字符串。好的,你的代码可以将所有{}更改为int,但现在我必须知道什么是int。服务器中有多少玩家在线?我需要更改所有{}在@I需要知道{server@和}之间是什么的情况下,服务器名中的玩家可以替换你的代码。非常感谢你的时间。你的正则表达式不起作用,所以我用(.*)更改了它奇怪。我试过了,效果很好。是不是在@之后允许空值?这可能会回答问题。但是,仅代码的答案不如记录代码或详细解释为什么此代码是问题的解决方案的答案有用。这可能会回答问题。但是,仅代码的答案不如用作记录代码或详细解释为什么此代码是问题的解决方案的答案。