1
        rule "doesnot match REGEX"
      when
           RuleActivator( targetMessage == "message" )
           $g: Parent(name matches "^[0-9]{4}[" "][A-Z]{2}[" "][0-9]{5}[" "]")
      then
           insert(new ValidationError(Validation($ROOT, $g, "name"), "SSDN"));
     end

Currently this rule will be throw error when the regex will be matched. I want to throw error when this Regex doesnot match. So, is there any method in drools for it. Or there is some other way?

1 Answers1

1

Use not matches:

The operator not matches:

The operator returns true if the String does not match the regular expression. The same rules apply as for the matches operator.

Judging by the requirements:

Position 1-4: numeric part of postal code - Position 5: blanc - Position 6-7: two uppercases (A2) - Position 8: blanc - Position 9-13: address (numeric, align to the left, to be filled up with spaces) - Position 14: blanc

Your regex should look like the following one:

^[0-9]{4}[ ][A-Z]{2}[ ][0-9]{5}[ ]
Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397
  • I didn't understand.here my requirement was - Position 1-4: numeric part of postal code - Position 5: blanc - Position 6-7: two uppercases (A2) - Position 8: blanc - Position 9-13: address (numeric, align to the left, to be filled up with spaces) - Position 14: blanc - Position 15-35: free text So, I had written " " where I needed free space like for 5th position – Robin Dhiman Oct 12 '15 at 11:28
  • I updated the regex. You do not need the double quote inside the character class. – Wiktor Stribiżew Oct 12 '15 at 11:31
  • Thanks a lot. That worked completely fine for me. Can u plz also suggest for how to restrict user till max 35 characters he can enter anything(even special characters) after the 14th character. – Robin Dhiman Oct 12 '15 at 11:50
  • It is very easy, use a `[\\s\\S]` construct or `.` with `(?s)` inline modifier and a limiting quantifier with the `$` end of line/string anchor: `(?s)^[0-9]{4}[ ][A-Z]{2}[ ][0-9]{5}[ ].{0,35}$`, or `^[0-9]{4}[ ][A-Z]{2}[ ][0-9]{5}[ ][\\s\\S]{0,35}$`. The `{0,35}` limiting quantifier means *match 0 to 35 characters matching the preceding subpattern*. – Wiktor Stribiżew Oct 12 '15 at 11:52
  • Thanks a lot for helping me so much. You really saved my lot of time. Plz suggest any best online tutorial for regex where I can also get knowledge like you. I was looking at W3schools but it wasn't that helpful. Again Thank you very muchhhh – Robin Dhiman Oct 12 '15 at 11:57
  • I do not know your level of regex knowledge :) so that I can only suggest doing all lessons at [regexone.com](http://regexone.com/), reading through [regular-expressions.info](http://www.regular-expressions.info), [regex SO tag description](http://stackoverflow.com/tags/regex/info) (with many other links to great online resources), and the community SO post called [What does the regex mean](http://stackoverflow.com/questions/22937618/reference-what-does-this-regex-mean). – Wiktor Stribiżew Oct 12 '15 at 12:00
  • There is no need to use `^` at the beginning and `$` at the end of a regular expression since `matches` calls Matcher.matches, which tries to match the entire string anyway. -- I feel that unnecessarily enclosing a single character in `[` and `]` does not improve readability, but you may feel different. – laune Oct 12 '15 at 14:29
  • The `^` and `$` are not necessary with `matches`, it is true. As for `[ ]`, this is just an extra safety measure in case a verbose modifier is used. – Wiktor Stribiżew Oct 12 '15 at 14:30