Search code examples
javatext

how to extract variables based on a pattern braces using regex in Java?


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,


Solution

  • 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.