Regular expressions: Difference between revisions From Online Manual

Jump to: navigation, search
(Regular expressions in SMF must be delimited in order to be evaluated)
 
(Creating headers to make it look like a nice wiki page! Also added some extra info about meta-characters, so that the example regex expressions make sense to the reader! Importance of the input mask not relevant here, that's mentioned elsewhere)
 
(7 intermediate revisions by 3 users not shown)
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:
In SMF2.0, regular expressions (regex) can be used in input masks as found in the ''[[SMF2.0:Features_and_Options#Advanced_Settings|Advanced Settings]]'' when creating or editing a custom profile field.


[12][0-9]{3}
==Using Regular Expressions==


it doesn't work. Even worse, it fails silently, acting as though '''any''' data that was entered is matched by the regex.
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: '''<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>'''


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:
===Examples===


~[12][0-9]{3}~
The following examples give an idea of what can be done with regular expressions:


then it works as expected. Double quotes also work as delimiters.
*'''<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.


Tested in SMF 2.0.4.
===Meta-characters===
 
In regular expressions you can use meta-characters to define the search for matches.
 
*'''?''' - <u>None or one</u> match of the preceding expression.
*'''+''' - <u>One or more</u> matches of the preceding expression.
*'''<nowiki>*</nowiki>''' - <u>None or more</u> matches of the preceding expression.
*'''{''n''}''' - Matches the preceding expression <u>exactly</u> ''n'' times.
*'''{''n'',}''' - Matches the preceding expression <u>at least</u> ''n'' times.
*'''{,''n''}''' - Matches the preceding expression <u>no more than</u> ''n'' times.
*'''{''n'',''m''}''' - Matches the preceding expression <u>at least</u> ''n'' times, but <u>no more than</u> ''m'' times.
*'''^''' - When used <u>outside</u> square brackets, it marks the start of the string. When used <u>inside</u> square brackets it has the affect of negating the class, but only if it is the first character.
*'''$''' - The end of the string.
*'''\''' - Escapes the next character.
 
[[Category: As an administrator]]

Latest revision as of 21:06, 13 September 2014

In SMF2.0, regular expressions (regex) can be used in input masks as found in the Advanced Settings when creating or editing a custom profile field.

Using Regular Expressions

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}"

Examples

The following examples give an idea of what can be done with 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.

Meta-characters

In regular expressions you can use meta-characters to define the search for matches.

  • ? - 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.
  • ^ - When used outside square brackets, it marks the start of the string. When used inside square brackets it has the affect of negating the class, but only if it is the first character.
  • $ - The end of the string.
  • \ - Escapes the next character.


Advertisement: