Search code examples
regexcoldfusioncoldfusion-10

Malformed regular expression in ColdFusion


My password should contain at least one special character from the list below.

@ % + / ! # $ ^ ? : , { } ( ) [ ] ~ - _.`

I tried this in ColdFusion using the below code but it is throwing hard error saying:

Malformed regular expression "@%+/!#$^?:,{}()[]~`-_"

<cfif REFind("@%+/!##$^?:,{}()[]~`-_",arguments.myPassword) IS 0>
 // some business logic
</cfif>

Could any one help me fix this issue?


Solution

  • Your regex is malformed because [`-_] forms an invalid range.

    Use

    <cfif REFind("[@%+/!##$^?:,{}()[\\]~`_.-]",arguments.myPassword) IS 0>
    

    Here, note that

    • a [...] is a character class that matches 1 char form the defined sets/symbols inside the class
    • - is at the end of the character class and is thus treated as a literal -
    • ] inside the character class MUST be escaped with a literal \