Regular expressions: Difference between revisions From Online Manual

Jump to: navigation, search
mNo edit summary
(Updating the section on quantifiers)
Line 11: Line 11:
*'''<code>"^([A-Fa-f0-9]{3}|[A-Fa-f0-9]{6})$" </code>''' - Only allow 3 or 6 character hexcodes.
*'''<code>"^([A-Fa-f0-9]{3}|[A-Fa-f0-9]{6})$" </code>''' - Only allow 3 or 6 character hexcodes.


Additionally, special metacharacters such as ?+*^$ and {xx} can be defined:
Additionally, special metacharacters such as '''?+*^$''' and '''{xx}''' can be defined:


*'''?''' - None or one match of previous expression.
*'''?''' - <u>None or one</u> match of the preceding expression.
*'''+''' - One or more of previous expression.
*'''+''' - <u>One or more</u> matches of the preceding expression.
*'''<nowiki>*</nowiki>''' - None or more of previous expression.
*'''<nowiki>*</nowiki>''' - <u>None or more</u> matches of the preceding expression.
*'''{xx}''' - An exact number from previous expression.
*'''{''n''}''' - Matches the preceding expression <u>exactly</u> ''n'' times.
*'''{xx,}''' - An exact number or more from previous expression.
*'''{''n'',}''' - Matches the preceding expression <u>at least</u> ''n'' times.
*'''{,xx}''' - An exact number or less from previous expression.
*'''{,''n''}''' - Matches the preceding expression <u>no more than</u> ''n'' times.
*'''{xx,yy}''' - An exact match between the two numbers from previous expression.
*'''{''n'',''m''}''' - Matches the preceding expression <u>at least</u> ''n'' times, but <u>no more than</u> ''m'' times.  
*'''^''' - Start of string.
*'''^''' - The start of the string.
*'''$''' - End of string.
*'''$''' - The end of the string.
*'''\''' - Escapes the next character.
*'''\''' - Escapes the next character.


[[Category: As an administrator]]
[[Category: As an administrator]]

Revision as of 11:55, 13 September 2014

In SMF2.0, regular expressions can be used in the Advanced Settings when creating or editing a custom profile field.

NOTE - When entering regular expressions (regex), you must surround them with delimiters such as tildes (~) or double quotes ("), otherwise they will not work and there will be no warning of this failure. Two examples of the correct way of entering regular expressions for SMF are: ~[12][0-9]{3}~ or "[12][0-9]{3}"

The input mask is important for your forum's security. Validating the input from a user can help to ensure that data is not used in a way you do not expect. Here are some examples of simple regular expressions:

  • "[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 such as ?+*^$ and {xx} can be defined:

  • ? - None or one match of the preceding expression.
  • + - One or more matches of the preceding expression.
  • * - None or more matches of the preceding expression.
  • {n} - Matches the preceding expression exactly n times.
  • {n,} - Matches the preceding expression at least n times.
  • {,n} - Matches the preceding expression no more than n times.
  • {n,m} - Matches the preceding expression at least n times, but no more than m times.
  • ^ - The start of the string.
  • $ - The end of the string.
  • \ - Escapes the next character.


Advertisement: