[SalesForce] Regular Expressions in APEX for a field

I'm new to APEX and was writing a validation function for a field called DESIGNATION. I have the REGEX that that looks like this:

(^[a-zA-Z0-9][a-zA-Z0-9=.\-\/\s+]{1,49})

The designation field must begin with an a-z, A-Z or 0-9 and be followed by 1 to 49 more characters that are a-z, A-Z, 0-9 an equal sign, a period, a hyphen, a backslash, or a space character. This field must be one character long and can be a total of 50 characters long.

Here is a screen shot of the validation screen:

Page Validation

When I go to run my app on the screen called SYNOPTIC with the designation field on it to validate and type "AZC A }{}##" which has the bad characters of '}{}##' in it, it allow the row to be updated and shouldn't

UI

So my question is, is it the regular expression that is the issue or the Type field being "Item/Column in Expression 1 matches Regular Expression in Expression 2", or a combination of both? If my regular expression is bad then how do I fix it?

Best Answer

The dot has a special meaning in regular expressions and so any character is valid. You need to escape the dot with a backslash for it to work. Moreover you stated that a backslash should be matched, but your expression would match a forward slash instead. To change this you need to replace \/ with \\. Altogether this leads to something like this:

(^[a-zA-Z0-9][a-zA-Z0-9=\.\-\\\s+]{1,49})

A good resource to test regular expressions that also gives a detailed explanation of the current meaning of the expression is regular expressions 101

Related Topic