我们能解析一个地形吗;变量.tf“;Java文件?如果是,如何实现相同的功能
请查找Variable.tf文件的示例片段,我只想获取订阅id、计划id、用户id。我不想获取位置,因为它已经设置了默认值我们能解析一个地形吗;变量.tf“;Java文件?如果是,如何实现相同的功能,java,terraform,Java,Terraform,请查找Variable.tf文件的示例片段,我只想获取订阅id、计划id、用户id。我不想获取位置,因为它已经设置了默认值 variable "subscription_id" { description = "My subscription id" } variable "plan_id" { description = "My tenant id" } variable "user_id" { description = "My user id" } variable "locatio
variable "subscription_id" {
description = "My subscription id"
}
variable "plan_id" {
description = "My tenant id"
}
variable "user_id" {
description = "My user id"
}
variable "location" {
type="string"
default="Sydney"
description="The name of the location in which your account is
created. "
}
您可以使用正则表达式查找各种变量,然后使用另一个正则表达式解析找到的变量。实现这一点的一种方法是:
Map<String, Map<String,String>> parse(String input){
HashMap<String, Map<String, String>> variables = new HashMap<>();
Matcher variableMatcher = Pattern.compile("variable \"(\\S+?)\"\\s*\\{\\s*((?:.|\\n)+?)\\s*}").matcher(input);
Pattern bodyElementPattern = Pattern.compile("(\\S+)\\s*=\\s*\"(.+)\"");
while (variableMatcher.find()){
HashMap<String, String> bodyElements = new HashMap<>();
Matcher bodyElementMatcher = bodyElementPattern.matcher(variableMatcher.group(2));
while (bodyElementMatcher.find()){
bodyElements.put(bodyElementMatcher.group(1),bodyElementMatcher.group(2));
}
variables.put(variableMatcher.group(1),bodyElements);
}
return variables;
}
映射解析(字符串输入){
HashMap变量=新的HashMap();
Matcher variableMatcher=Pattern.compile(“变量\”(\\S+?)\”\\S*\\{\\S*((?:。\\\n)+?)\\S*}”).Matcher(输入);
Pattern bodyelement Pattern=Pattern.compile(“(\\S+)\\S*=\\S*\”(.+)\”);
while(variableMatcher.find()){
HashMap bodyElements=新的HashMap();
Matcher bodyElementMatcher=bodyElementPattern.Matcher(variableMatcher.group(2));
while(bodyElementMatcher.find()){
bodyElementMatcher.group(1),bodyElementMatcher.group(2));
}
变量.put(variableMatcher.group(1),bodyElements);
}
返回变量;
}
在这里,您将获得变量名称到变量内容的映射,其中变量内容表示为组件名称和值的另一个映射。您可以使用正则表达式查找各种变量,然后使用另一个正则表达式解析找到的变量。实现这一点的一种方法是:
Map<String, Map<String,String>> parse(String input){
HashMap<String, Map<String, String>> variables = new HashMap<>();
Matcher variableMatcher = Pattern.compile("variable \"(\\S+?)\"\\s*\\{\\s*((?:.|\\n)+?)\\s*}").matcher(input);
Pattern bodyElementPattern = Pattern.compile("(\\S+)\\s*=\\s*\"(.+)\"");
while (variableMatcher.find()){
HashMap<String, String> bodyElements = new HashMap<>();
Matcher bodyElementMatcher = bodyElementPattern.matcher(variableMatcher.group(2));
while (bodyElementMatcher.find()){
bodyElements.put(bodyElementMatcher.group(1),bodyElementMatcher.group(2));
}
variables.put(variableMatcher.group(1),bodyElements);
}
return variables;
}
映射解析(字符串输入){
HashMap变量=新的HashMap();
Matcher variableMatcher=Pattern.compile(“变量\”(\\S+?)\”\\S*\\{\\S*((?:。\\\n)+?)\\S*}”).Matcher(输入);
Pattern bodyelement Pattern=Pattern.compile(“(\\S+)\\S*=\\S*\”(.+)\”);
while(variableMatcher.find()){
HashMap bodyElements=新的HashMap();
Matcher bodyElementMatcher=bodyElementPattern.Matcher(variableMatcher.group(2));
while(bodyElementMatcher.find()){
bodyElementMatcher.group(1),bodyElementMatcher.group(2));
}
变量.put(variableMatcher.group(1),bodyElements);
}
返回变量;
}
在这里,您将获得变量名称到变量内容的映射,其中变量内容表示为组件名称和值的另一个映射。@AUsername我批准了您的编辑,但最好让OP自己学习如何将代码作为代码发布,而不是在将来为他发布代码。因此,您需要向我们展示您首先做了什么。@AUsername我批准了您的编辑,但是最好让OP自己学习如何将代码作为代码发布,而不是在将来为他这样做。因此,您需要向我们展示您首先做了什么。