Java 从字符串中提取占位符
我有两条线。一个包含“格式”和占位符,而另一个包含占位符的实际值 例如: 字符串一:Java 从字符串中提取占位符,java,string,replace,split,Java,String,Replace,Split,我有两条线。一个包含“格式”和占位符,而另一个包含占位符的实际值 例如: 字符串一:“ 字符串二:“我的用户我的通行证” 字符串一:,“ 字符串二:“约翰,史密斯” 我试图将变量字符串username分配给第二个字符串中用户名占位符的值,将变量字符串password分配给第二个字符串中密码占位符的值 我知道String.replaceAll()方法,但这不就是用第二个字符串替换第一个字符串吗?我建议继续使用regex进行此操作。首先捕获字符串1中的所有键并创建相应的正则表达式,从中获取字符串2中
“
字符串二:“我的用户我的通行证”
字符串一:,“
字符串二:“约翰,史密斯”
我试图将变量字符串username
分配给第二个字符串中用户名占位符的值,将变量字符串password
分配给第二个字符串中密码占位符的值
我知道
String.replaceAll()
方法,但这不就是用第二个字符串替换第一个字符串吗?我建议继续使用regex进行此操作。首先捕获字符串1中的所有键并创建相应的正则表达式,从中获取字符串2中的值
用于提取键和相应值的代码:
String keyString = "<name>, <familyName>";
int i = 0;
List<String> keys = new ArrayList<>();
Pattern pattern = Pattern.compile("<(\\w+)>");
Matcher matcher = pattern.matcher(keyString);
StringBuilder sb = new StringBuilder("");
while(matcher.find()) {
String token = matcher.group(1);
sb.append(keyString.substring(i, matcher.start()));
sb.append("([\\w ]+)");
keys.add(token);
i = matcher.end();
}
Pattern valuePattern = Pattern.compile(sb.toString());
Matcher valueMatcher = valuePattern.matcher("John, Smith");
Map<String,String> keyValueMap = new HashMap<>();
while(valueMatcher.find()) {
int counter = 0;
for(String key:keys) {
keyValueMap.put(key, valueMatcher.group(++counter));
}
}
一种可能可行的方法是维护代币及其替代品的地图。然后,您可以迭代该映射并将替换应用于文本,如下所示:
String text = "There is a user <username> who has a password <password>";
Map<String, String> tokens = new HashMap<>();
tokens.put("<username>", "myUser");
tokens.put("<password>", "myPass");
for (Map.Entry<String, String> entry : tokens.entrySet()) {
text = text.replaceAll(entry.getKey(), entry.getValue());
}
System.out.println(text);
There is a user myUser who has a password myPass
String text=“有一个用户拥有密码”;
Map tokens=newhashmap();
tokens.put(“,“myUser”);
代币。put(“,“myPass”);
for(Map.Entry:tokens.entrySet()){
text=text.replaceAll(entry.getKey(),entry.getValue());
}
System.out.println(文本);
有一个用户myUser拥有密码myPass
这解决了一个普通的
String::replaceAll
方法:
String one ="<username> <password>";
String two = "<name>, <familyName>";
String username = "myUser";
String password = "myPass";
String name = "John";
String familyName = "Smith";
// will result in "myUser myPass" without the quotation marks
String outputOne = one.replaceAll("<username>", username).replaceAll("<password>", password);
// will result in "John Smith" without the quotation marks
String outputTwo = two.replaceAll("<name>", name).replaceAll("<familyName>", familyName);
stringone=”“;
字符串2=“,”;
字符串username=“myUser”;
字符串password=“myPass”;
字符串name=“John”;
字符串familyName=“Smith”;
//将生成不带引号的“myUser myPass”
字符串outputOne=1.replaceAll(“,用户名).replaceAll(“,密码);
//将生成不带引号的“John Smith”
字符串输出wo=two.replaceAll(“,name).replaceAll(“,familyName);
欢迎访问该网站。你应该尝试一些东西,即使你认为它不会产生预期的结果。这将有助于其他人理解你正在尝试做什么。目前,解释不够清晰,无法理解您尝试执行的具体操作。请使用,而不是全部替换。对于这种替换,正则表达式太重,不必要的复杂。欢迎使用StackOverflow。对不起,这不是这个网站的工作方式。如中所述,此站点是有用问题及其答案的存储库,而不是帮助论坛。请参加,访问,特别是阅读和学习如何有效地使用这个网站。嗨,谢谢。我不习惯论坛,或者stackoverflow。我会查看这些链接,试着习惯这个网站的工作方式。听起来不错。你能详细说明一下吗?例如,显示完整的代码示例?据我所知,OP希望在文本中找到例如
占位符,然后用一些已知值替换它。您的正则表达式代码只是在查找占位符,但我看不到任何替换发生。@TimBiegeleisen我想OP想要创建一个键值对,我之前分享了一个想法,但现在我也用code@Zabuza我已经编辑了我的答案,并与输出共享了代码片段。希望这对你有帮助投票是在编辑之前。所以我猜是因为你当时的回答没有完全解决这个问题。这只是一个暗示或评论。现在好多了,让我投赞成票:)
String one ="<username> <password>";
String two = "<name>, <familyName>";
String username = "myUser";
String password = "myPass";
String name = "John";
String familyName = "Smith";
// will result in "myUser myPass" without the quotation marks
String outputOne = one.replaceAll("<username>", username).replaceAll("<password>", password);
// will result in "John Smith" without the quotation marks
String outputTwo = two.replaceAll("<name>", name).replaceAll("<familyName>", familyName);