Home > Software design >  Regex for validating comma separated java fields, referenced fields, and Sets
Regex for validating comma separated java fields, referenced fields, and Sets

Time:02-12

I have a code which parses comma separated names of java class fields, and optionally field names with class names and dot operator(i.e. foreign fields) and also set/array like notation to refer to multiple foreign fields without using class name multiple times: e.g. following are valid scenarios:

  1. MyField1,MyField2,AnotherClass.ACField1,AnotherClass2[AC2Field1,AC2Field2,AC2Field3]
  2. MyField1,MyField2

I have written following regex/java code: But see the last line in main method (i.e. first example above). It should be valid but regex is not matching.

import java.util.regex.Pattern;

public class RegexTest {

  static Pattern fieldListPattern = Pattern.compile("^(\\w )(([.,]\\w )|([\\[]\\w ([,]?\\w )[\\]]))*$");

  public static void main(String[] args) {
    validate("MyField1,MyField2"); // valid
    validate("ABC,A.CDE"); // valid
    validate("ABC"); // valid
    validate("ABC,CDE"); // valid
    validate("ABC.CDE"); // valid
    validate("ABC,.CDE"); // XXXXXXX
    validate("ABC.,CDE"); // XXXXXXX
    validate(",ABC,CDE"); // XXXXXXX
    validate(",ABC"); // XXXXXXX
    validate(".ABC"); // XXXXXXX
    validate(".ABC.CDE"); // XXXXXXX
    validate(".ABC,CDE"); // XXXXXXX
    validate(",ABC.CDE"); // XXXXXXX

    // arrays

    validate("ABC[CDE]"); // Valid
    validate("ABC[]"); // XXXXXXX
    validate("ABC.[CDE]"); // XXXXXXX
    validate("MyField1,MyField2,AnotherClass.ACField1,AnotherClass2[AC2Field1,AC2Field2]"); // Valid
    validate("MyField1,MyField2,AnotherClass.ACField1,AnotherClass2[AC2Field1,AC2Field2,AC2Field3]"); // This should be valid, but coming as invalid
  }

  static void validate(String s) {
    System.out.println(s);
    fieldListPattern.matcher("").matches();
    System.out.println("Valid ? "   fieldListPattern.matcher(s).matches());
  }
}

CodePudding user response:

As a pattern, you can optionally repeat matching from an opening till closing square bracket:

^\w (?:\[\w (?:,\w )*\])?(?:[.,]\w (?:\[\w (?:,\w )*\])?)*$
  • ^ Start of string
  • \w Match 1 word chars
  • (?:\[\w (?:,\w )*\])? Optionally match word characters separated by a comma in between square brackets
  • (?: Non capture group
    • [.,] Match either . or ,
    • \w (?:\[\w (?:,\w )*\])*
  • )? Close the group and make it optional
  • $ End of string

Regex demo

In Java with the double escaped backslashes:

String regex = "^\\w (?:\\[\\w (?:,\\w )*\\])?(?:[.,]\\w (?:\\[\\w (?:,\\w )*\\])?)*$";
  • Related