This should help:
String s = "//name:value /name:value // name:value";
Pattern p = Pattern.compile("//?\\s*(\\w+):(\\w+)\\s*");
Matcher m = p.matcher(s);
while (m.find()) {
System.out.printf("%s = '%s'%n", m.group(1), m.group(2));
}
Some notes:
- It is assumed the
/ or // can delineate name:value pairs;
- Optional white space after
/ or // and after name:value is ignored;
- Both
name and value are captured. You don't say what you want to capture. Adjust the parentheses as necessary;
- Both
name and value consist of A-Z, a-z, 0-9 or _ (that's what \w means).
If you don't want to find the values but simply test for the validity as a whole:
String s = "//name:value /name:value // name:value";
if (s.matches("(//?\\s*\\w+:\\w+\\s*)+")) {
// it fits
}