如何使用Java中的正则表达式基于花括号模式提取变量?

huangapple go评论59阅读模式
英文:

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= &quot;anyvalue{variable1}anyvalue{variable2}anyvalue{var3}&quot;;
        String input = &quot;anyvaluehelloanyvalueworldanyvaluesomething&quot;;

        // Convert the pattern into a regular expression
        String regex = pattern.replaceAll(&quot;\\{(.+?)\\}&quot;, &quot;(?&lt;$1&gt;.*)&quot;);

        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(&quot;\\(\\?&lt;(.+?)&gt;&quot;);
            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 + &quot;: &quot; + variableValue);
            }
        } else {
            System.out.println(&quot;No match found.&quot;);
        }
    }
}

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.

huangapple
  • 本文由 发表于 2023年4月7日 00:29:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75951761.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定