1

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
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Harmeet Singh Taara
  • 6,483
  • 20
  • 73
  • 126
  • Do you mean a pattern like `CH.{4,}` or `CH.*`? (Note the extra `.` in my expression.) Why do you want to match something like `CHHHHHHH`? Are you working on a chemical application? – Gábor Bakos May 02 '15 at 14:36
  • 1
    Could it be the missing `.` in the regular expressions `CH.{4,}` and `CH.*`? – Gregor Raýman May 02 '15 at 14:37
  • @GáborBakos its just create for sample application, for testing how regular expressions are work with `deadbolt`. – Harmeet Singh Taara May 02 '15 at 15:37
  • @HarmeetSinghTaara I am pretty sure they work similarly to other programs. What I tried to express: It is not clear what you want to achieve. – Gábor Bakos May 02 '15 at 15:39
  • @GregorRaýman this is work for me. My `CH.*` and `CH.{4,}` runs successfully. But still i am not getting, why we need `.` for regular expressions ? – Harmeet Singh Taara May 02 '15 at 15:52
  • Deadbolt compiles regexs using the standard Java library: `java.util.regex.Pattern.compile(patternValue)`. See https://github.com/schaloner/deadbolt-2-scala/blob/master/code/app/be/objectify/deadbolt/scala/DeadboltActions.scala#L117-L122 and https://github.com/schaloner/deadbolt-2-scala/blob/master/code/app/be/objectify/deadbolt/scala/DeadboltActions.scala#L137 for implementation. – Steve Chaloner May 04 '15 at 06:00

2 Answers2

4

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.*.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
2

You need to use CH.* or CH.{4,} if you want something (not just Hs) after CH. The . means any character, just like in any other regular expression.

Gábor Bakos
  • 8,982
  • 52
  • 35
  • 52