Regular expressions: Difference between revisions From Online Manual

Jump to: navigation, search
(Regular expressions in SMF must be delimited in order to be evaluated)
 
No edit summary
Line 1: Line 1:
In Custom Profile Fields the Input Mask can be set to 'Regex (Advanced)', which then allows you to enter a regular expression that you would like to use to validate that profile field. However, the regex that you enter must be delimited by special characters. If you simply enter a regex, like this:
Regular expressions can be used in the advanced profile fields/custom profile fields.


[12][0-9]{3}
The input mask is important for your forum's security. Validating the input from a user can help ensure that data is not used in a way you do not expect. We have provided some simple regular expressions as hints.


it doesn't work. Even worse, it fails silently, acting as though '''any''' data that was entered is matched by the regex.
"[A-Za-z]+" - Match all upper and lower case alphabet characters.
"[0-9]+" - Match all numeric characters.
"[A-Za-z0-9]{7}" - Match all upper and lower case alphabet and numeric characters seven times.
"[^0-9]?" - Forbid any number from being matched.
"^([A-Fa-f0-9]{3}|[A-Fa-f0-9]{6})$" - Only allow 3 or 6 character hexcodes.


If you enter the regex surrounded by tildes, as shown by [http://www.simplemachines.org/community/index.php?topic=485300.msg3398080#msg3398080[emanuele]], or like this:


~[12][0-9]{3}~
Additionally, special metacharacters ?+*^$ and {xx} can be defined.
? - None or one match of previous expression.
+ - One or more of previous expression.
* - None or more of previous expression.
{xx} - An exact number from previous expression.
{xx,} - An exact number or more from previous expression.
{,xx} - An exact number or less from previous expression.
{xx,yy} - An exact match between the two numbers from previous expression.
^ - Start of string.
$ - End of string.
\ - Escapes the next character.


then it works as expected. Double quotes also work as delimiters.


Tested in SMF 2.0.4.
More information and advanced techniques may be found on the internet.

Revision as of 12:48, 11 June 2013

Regular expressions can be used in the advanced profile fields/custom profile fields.

The input mask is important for your forum's security. Validating the input from a user can help ensure that data is not used in a way you do not expect. We have provided some simple regular expressions as hints.

"[A-Za-z]+" - Match all upper and lower case alphabet characters. "[0-9]+" - Match all numeric characters. "[A-Za-z0-9]{7}" - Match all upper and lower case alphabet and numeric characters seven times. "[^0-9]?" - Forbid any number from being matched. "^([A-Fa-f0-9]{3}|[A-Fa-f0-9]{6})$" - Only allow 3 or 6 character hexcodes.


Additionally, special metacharacters ?+*^$ and {xx} can be defined. ? - None or one match of previous expression. + - One or more of previous expression.

  • - None or more of previous expression.

{xx} - An exact number from previous expression. {xx,} - An exact number or more from previous expression. {,xx} - An exact number or less from previous expression. {xx,yy} - An exact match between the two numbers from previous expression. ^ - Start of string. $ - End of string. \ - Escapes the next character.


More information and advanced techniques may be found on the internet.



Advertisement: