简体   繁体   中英

Key=Value pair validation using Regular Expression

Need to validate the following string using regular expression:

Key=Value;Key=Value;Key=Value and so on

For example: MAC Address=192.167.235.19; zproduct_id=XYZ MAC Address=192.167.235.19; zproduct_id=XYZ

  1. Key and value can contain anything except = and ;
  2. Key and Value both cannot be blank or empty with sapces
  3. Input string does not ends with ;

Tried with the following regular expression

Pattern pattern1 = Pattern.compile("(([^=;]*)=([^=;]*);?)+");
Matcher matcher = pattern1.matcher("MAC Address=192.167.235.19; zproduct_id=XYZ");
if (matcher.matches())
System.out.println("Match");
else
System.out.println("NOT");

Below is validation result of above regular expression with Input Strings

Input String                                      Validation Result  Expected Result

1. MAC Address=192.167.235.19                         Match              Match
2. MAC Address=192.167.235.19; zproduct_id=XYZ        Match              Match
3. MAC Address=192.167.235.19;                        Match              Not
4. MAC =Address=192.167.235.19; zproduct_id=XYZ       Match              Not
5. MAC Address=; zproduct_id=XYZ                      Match              Not  
6. MAC Address=     ; zproduct_id=XYZ                 Match              Not  
7.     =192.167.235.19 ; zproduct_id=XYZ              Match              Not

Please suggest a regular expression which shall validate all above scenario for the expected results.

Thanks in Advance

This is a little more elaborate.

Edit this one enforces that Key must be a value that is not a whitespace (nor empty).
Otherwise, =;=;=;=;=;=;=;= is going to be valid.
So this would be valid Key1 = ;Key2=val; Key1= val Key1 = ;Key2=val; Key1= val

 # "^(?:\\s*((?:[^=;\\s]+\\s+)*[^=;\\s]+)\\s*=\\s*([^=;]*)(?:;(?!\\s*$)|$))+$"

 ^ 
 (?:
      \s* 
      (                             # (1 start)
           (?: [^=;\s]+ \s+ )*
           [^=;\s]+ 
      )                             # (1 end)
      \s* 
      =
      \s* 
      ( [^=;]* )                    # (2)
      (?:
           ; (?! \s*  $ )
        |  $ 
      )

 )+
 $

Edit2 This enforces that Key and Value cannot be whitespace (nor empty).

 #   "^(?:\\s*((?:[^=;\\s]+\\s+)*[^=;\\s]+)\\s*=\\s*((?:[^=;\\s]+\\s+)*[^=;\\s]+)\\s*(?:;(?!\\s*$)|$))+$"

 ^ 
 (?:
      \s* 
      (                             # (1 start), Key
           (?: [^=;\s]+ \s+ )*
           [^=;\s]+ 
      )                             # (1 end)
      \s* 
      =
      \s* 
      (                             # (2 start), Value
           (?: [^=;\s]+ \s+ )*
           [^=;\s]+ 
      )                             # (2 end)
      \s* 
      (?:
           ;
           (?! \s*  $ )
        |  $ 
      )
 )+
 $ 

Use this regex:

Pattern pattern1 = Pattern.compile("([^=;]*)=([^=;]*)(?:;|$) *");

Instead of:

if (matcher.matches()) {...}

Use Matcher#find() :

while ( matcher.find() ) {
   System.out.println("key: " + matcher.group(1) + ", value: " + matcher.group(2));
}

Try with the next regular expression:

private static final Pattern REGEX_PATTERN = 
        Pattern.compile("^(([^=;]+)=([^=;]+);?\\s*)*$");

From https://www.debuggex.com/

在此处输入图片说明

From your example it looks like every key=value pair (except last one) should have ; after it. To express this fact try changing your regex and replace ;? with (;(?!$)|$) (it will accept ; only if there is no end of data after it, OR will accept end of data).

Also if key or value cant be empty change * to + .

Try this regex "([^=;]+=[^=;]+(;(?!$)|$))+"


Demo:

String[] tests = { 
        "MAC Address=192.167.235.19",
        "MAC Address=192.167.235.19; zproduct_id=XYZ",
        "MAC Address=192.167.235.19; ",
        "MAC =Address=192.167.235.19; zproduct_id=XYZ",
};

for (String s : tests)
    System.out.println(s + " -> " + s.matches("([^=;]+=[^=;]+(;|$))+"));

Output:

MAC Address=192.167.235.19 -> true
MAC Address=192.167.235.19; zproduct_id=XYZ -> true
MAC Address=192.167.235.19;  -> false
MAC =Address=192.167.235.19; zproduct_id=XYZ -> false

Although a regex provides the most concise answer, it might be more straightforward to validate by looping over substrings:

final List<String> inputList = Arrays.asList(
    "MAC Address=192.167.235.19",
    "MAC Address=192.167.235.19; zproduct_id=XYZ",
    "MAC Address=192.167.235.19;",
    "MAC =Address=192.167.235.19; zproduct_id=XYZ"
);

for (String str : inputList) {
    System.out.println(str);
    final String[] keyVals = str.split(";", -1);
    boolean valid = keyVals.length > 0;
    for (String keyVal : keyVals) valid &= keyVal.split("=", -1).length == 2;
    System.out.println(valid);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM