Regular expressions: Difference between revisions From Online Manual

Jump to: navigation, search
No edit summary
(Tidying up, improving the layout of the existing information.)
Line 1: Line 1:
Regular expressions can be used in the advanced profile fields/custom profile fields.
In SMF2.0, regular expressions can be used in the ''[[SMF2.0:Features_and_Options#Advanced_Settings|Advanced Settings]]'' when creating or editing a [[SMF2.0:Features_and_Options#Custom_Profile_Fields|custom profile field]].


If you simply enter a regex, like this: <code>[12][0-9]{3}</code>
'''NOTE''' - When entering regular expression (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: '''<span style="color:#FF0000">~</span>'''[12][0-9]{3}'''<span style="color:#FF0000">~</span>''' or '''<span style="color:#FF0000">"</span>'''[12][0-9]{3}'''<span style="color:#FF0000">"</span>'''
it will not work. Even worse, it fails silently, acting as though '''any''' data that was entered is matched by the regex.


If you enter the regex surrounded by tildes like this: <code>~[12][0-9]{3}~</code> then it works as expected. Double quotes also work as delimiters.
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. Here ares ome examples of simple regular expressions:


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


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.
Additionally, special metacharacters such as ?+*^$ and {xx} can be defined:
 
<code>"[A-Za-z]+"</code> - Match all upper and lower case alphabet characters.
<code>"[0-9]+"</code> - Match all numeric characters.
<code>"[A-Za-z0-9]{7}"</code> - Match all upper and lower case alphabet and numeric characters seven times.
<code>"[^0-9]?"</code> - Forbid any number from being matched.
<code>"^([A-Fa-f0-9]{3}|[A-Fa-f0-9]{6})$" </code>- 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.


*'''?''' - None or one match of previous expression.
*'''+''' - One or more of previous expression.
*'''<nowiki>*</nowiki>''' - 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.


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

Revision as of 08:42, 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 expression (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: ~[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 ensure that data is not used in a way you do not expect. Here ares ome 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 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.


Advertisement: