0

I have a string of the format, {Key=value, Key=value.....} for which I have to write a validator. The above key-value are separated by space {Key=value, Key=value, Key=value.....}. I am completely new to regex but referring to one of the answers in Stack overflow I have written:

Boolean check = Pattern.matches("(\\{+)?(\\w+)?=(\\w+)?,?","string to be validated");

But the solution is not working.

It is also possible that the expected string could either be empty() or of the exact type <null>?

Any help is appreciated.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • You should use a grouping construct to be able to repeat a sequence of patterns. Like `text.matches("(?:\\{\\w+=\\w+(?:,\\w+=\\w+)*})?")`. You can't check if an object is *null* with regex, regex only works with strings. – Wiktor Stribiżew Feb 01 '21 at 11:01
  • Using matches you should match the whole string. – The fourth bird Feb 01 '21 at 11:08
  • @WiktorStribiżew just tried System.out.println(Pattern.matches("\\{\\w+=\\w+(?:,\\w+=\\w+)*}","{A=B, C=D}")); but it didn't worked – MayurJunior Feb 01 '21 at 11:10
  • You added spaces to the sample text in the question, so you need `text.matches("(?:\\{\\w+=\\w+(?:\\s*,\\s*\\w+=\\w+)*})?")` – Wiktor Stribiżew Feb 01 '21 at 11:11
  • @Thefourthbird can you please suggest an example for above string? – MayurJunior Feb 01 '21 at 11:11
  • You can use a regex tester like https://regex101.com/r/vIEJDD/1 to see the match. Using matches, you should match the whole string. – The fourth bird Feb 01 '21 at 11:15
  • [This is my regex suggestion demo](https://regex101.com/r/vIEJDD/3). – Wiktor Stribiżew Feb 01 '21 at 11:46
  • @Thefourthbird your solution is working perfectly fine for me but I just realized that sometime key value are in form of "{Key=Value,Key=key=value}" as well. Can you please suggest, how can we handle such scenarios as well? – MayurJunior Feb 01 '21 at 12:09
  • @MayurJunior - Why don't you simply split the string on `,` and then check if each element in the resulting array matches `\w+\s*=\s*\w+`? – Arvind Kumar Avinash Feb 01 '21 at 12:11
  • @MayurJunior That is an odd looking key value pair, but like this would match it https://regex101.com/r/axQStv/1 – The fourth bird Feb 01 '21 at 12:14
  • LiveandLetLive your solution is the generic one and will work for all the cases but its too lengthy. I just wanted to validated the whole string in single line. @Thefourthbird the solution which you provided only work for {Key=value, Key=value} i.e. 2 pairs only but my string can contain "n" numbers of such pairs(which was my initial question). Could you please provide me the single line validation for such string? – MayurJunior Feb 01 '21 at 14:47
  • @MayurJunior - You can put the whole logic in a function. I've updated the answer accordigly. – Arvind Kumar Avinash Feb 01 '21 at 21:39

1 Answers1

3

You can split the string on \s*,\s* and then check if each element in the resulting array matches \w+\s*=\s*\w+.

public class Main {
    public static void main(String[] args) {
        // Test strings
        String[] arr = { "{Key1=value1, Key2=value2}", "{Key=Value,Key=key=value}" };

        boolean valid = true;
        for (String s : arr) {
            System.out.println(s + " => " + (!isValid(s) ? "false" : "true"));
        }
    }

    static boolean isValid(String str) {
        boolean valid = true;
        String regex = "\\w+\\s*=\\s*\\w+";
        String[] entries = str.replaceAll("[\\{\\}]", "").split("\\s*,\\s*");
        for (String entry : entries) {
            if (!entry.matches(regex)) {
                valid = false;
                break;
            }
        }
        return valid;
    }
}

Output:

{Key1=value1, Key2=value2} => true
{Key=Value,Key=key=value} => false

Note:

  1. \s*,\s* specifies zero or more whitespace characters followed by a comma which in turn may be followed by zero or more whitespace characters.
  2. \w+\s* specifies one or more word characters followed by zero or more whitespace characters.
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110