regexscalaplayframework-2.3deadbolt-2

Playframework with Deadbolt 2: Pattern regular expression not match


I am using Deadbolt2 with play-framework 2.3.x. When I am trying to access the controller with declare deadbolt Patterns using regular expressions. I am getting Not-found error. According to this sample, it is possible to use regular expressions with Pattern in our application. But when I declare a regular expression, I am not able to use it. My code looks like this:

def pattern_one = Pattern("CH{4,}", PatternType.REGEX, new MyDeadboltHandler) {} // NOT ACCESSED 

def pattern_one = Pattern("CH*", PatternType.REGEX, new MyDeadboltHandler) {  // NOT ACCESSED

def pattern_one = Pattern("CHANNEL", PatternType.REGEX, new MyDeadboltHandler) { // ACCESSED SUCCESSFULLY

Solution

  • Regular expressions are not wildcards. If a * wildcard matches anything any number of times, in regex, you need to use .*, where . means any character but a newline, and * means 0 or more times.

    More, if you want to match the whole string that contains a word in a string starting with CH, you can use a word boundary, \\b: \\bCH.*.

    If you want to specify that the string must start with CH and match the whole string, you can use ^CH.*.