英文:
how to extract variables based on a pattern braces using regex in Java?
问题
pattern: anyValue{variable1}otherValue{variable2}lastValue
value: anyValuehellootherValueworldlastValue
Expect this output
variable1: hello
variable2: world
can i have any variables with braces,
I would like an example using Regex with an implementation in Java or another language,
英文:
Have this data input:
pattern: anyValue{variable1}otherValue{variable2}lastValue
value: anyValuehellootherValueworldlastValue
Expect this output
variable1: hello
variable2: world
can i have any variables with braces,
I would like an example using Regex with an implementation in Java or another language,
答案1
得分: 0
以下是代码的中文翻译部分:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
String pattern = "anyvalue{variable1}anyvalue{variable2}anyvalue{var3}";
String input = "anyvaluehelloanyvalueworldanyvaluesomething";
// 将模式转换为正则表达式
String regex = pattern.replaceAll("\\{(.+?)\\}", "(?<$1>.*)");
Pattern r = Pattern.compile(regex);
Matcher m = r.matcher(input);
if (m.find()) {
// 从模式中获取捕获组的名称
Pattern groupNamesPattern = Pattern.compile("\\(\\?(<$1>.+?)");
Matcher groupNamesMatcher = groupNamesPattern.matcher(regex);
// 打印找到的所有变量
while (groupNamesMatcher.find()) {
String variableName = groupNamesMatcher.group(1);
String variableValue = m.group(variableName);
System.out.println(variableName + ": " + variableValue);
}
} else {
System.out.println("未找到匹配。");
}
}
}
请注意,代码的中文翻译仅包括代码部分,不包括您提供的其他文本内容。
英文:
You can use the following code in java:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
String pattern= "anyvalue{variable1}anyvalue{variable2}anyvalue{var3}";
String input = "anyvaluehelloanyvalueworldanyvaluesomething";
// Convert the pattern into a regular expression
String regex = pattern.replaceAll("\\{(.+?)\\}", "(?<$1>.*)");
Pattern r = Pattern.compile(regex);
Matcher m = r.matcher(input);
if (m.find()) {
// Get the names of the capture groups from the pattern
Pattern groupNamesPattern = Pattern.compile("\\(\\?<(.+?)>");
Matcher groupNamesMatcher = groupNamesPattern.matcher(regex);
// Print all the variables that were found
while (groupNamesMatcher.find()) {
String variableName = groupNamesMatcher.group(1);
String variableValue = m.group(variableName);
System.out.println(variableName + ": " + variableValue);
}
} else {
System.out.println("No match found.");
}
}
}
I am basically converting your pattern to named groups for any {variable_name} in the pattern. As far as I know, there is no easy way to get the groups' names, so you need a second regex to get those for your output.
However, there seem to be a few alternatives to find the named groups. You can follow this thread if you want to look into other alternatives to find the names of the groups.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论