Actions

Using regular expressions: Difference between revisions

From LimeSurvey Manual

No edit summary
(Marked this version for translation)
Line 1: Line 1:
<languages /> <translate>
<languages /> <translate>


<!--T:1-->
__TOC__
__TOC__


<!--T:2-->
(Perl) Regular Expressions must start and finish with a forward slash ("/"). You can find a good library of regular expressions at http://www.regxlib.net/. These patterns will almost all work if surrounded with the forward slash.
(Perl) Regular Expressions must start and finish with a forward slash ("/"). You can find a good library of regular expressions at http://www.regxlib.net/. These patterns will almost all work if surrounded with the forward slash.


<!--T:3-->
To test your regex you can [http://www.regxlib.net/RETester.aspx use this regex tester].
To test your regex you can [http://www.regxlib.net/RETester.aspx use this regex tester].


<!--T:4-->
''Please add successfully tested regular expressions!''
''Please add successfully tested regular expressions!''


<!--T:5-->
Examples (note that these are all one line):
Examples (note that these are all one line):


=SPECIAL NOTE: Regular Expressions in Conditions=
=SPECIAL NOTE: Regular Expressions in Conditions= <!--T:6-->


<!--T:7-->
Note that when using regular expressions in the condition editor, do NOT include the beginning and ending slash.
Note that when using regular expressions in the condition editor, do NOT include the beginning and ending slash.


=Email Validation:=
=Email Validation:= <!--T:8-->


  <nowiki>/(\w[-._\w]*\w@\w[-._\w]*\w\.\w{2,3})/</nowiki>
  <!--T:9-->
<nowiki>/(\w[-._\w]*\w@\w[-._\w]*\w\.\w{2,3})/</nowiki>


<!--T:10-->
The above regex will not validate email adresses with "+" - which is valid, and e.g. promoted by gmail. The below regex is slightly modified and allows "+":
The above regex will not validate email adresses with "+" - which is valid, and e.g. promoted by gmail. The below regex is slightly modified and allows "+":


  <nowiki>/^(\w[-._+\w]*\w@\w[-._\w]*\w\.\w{2,3})$/</nowiki>
  <!--T:11-->
<nowiki>/^(\w[-._+\w]*\w@\w[-._\w]*\w\.\w{2,3})$/</nowiki>


<!--T:12-->
It is still far from perfect - but I think its better then the original (also because start-of-string and end-of-string anchors are added). Mind. This will allow e.g. abc+++defg@somemail.office.com. Ideally the regex should be modified to only allow one "+" charachter.
It is still far from perfect - but I think its better then the original (also because start-of-string and end-of-string anchors are added). Mind. This will allow e.g. abc+++defg@somemail.office.com. Ideally the regex should be modified to only allow one "+" charachter.


=Postcodes:=
=Postcodes:= <!--T:13-->


==Australian Postcodes:==
==Australian Postcodes:== <!--T:14-->


  <nowiki>/^[0-9]{4}/</nowiki>
  <!--T:15-->
<nowiki>/^[0-9]{4}/</nowiki>


==Brazilian Postcodes:==
==Brazilian Postcodes:== <!--T:16-->


  <nowiki>/^[0-9]{2}\.[0-9]{3}-[0-9]{3}$/</nowiki>
  <!--T:17-->
<nowiki>/^[0-9]{2}\.[0-9]{3}-[0-9]{3}$/</nowiki>


==Canadian Postcodes:==
==Canadian Postcodes:== <!--T:18-->


  <nowiki>/^[a-zA-Z]\d{1}[a-zA-Z](\-| |)\d{1}[a-zA-Z]\d{1}$/</nowiki>
  <!--T:19-->
<nowiki>/^[a-zA-Z]\d{1}[a-zA-Z](\-| |)\d{1}[a-zA-Z]\d{1}$/</nowiki>


==US Postal Codes:==
==US Postal Codes:== <!--T:20-->


  <nowiki>/^[0-9]{5}([- /]?[0-9]{4})?$/</nowiki>
  <!--T:21-->
<nowiki>/^[0-9]{5}([- /]?[0-9]{4})?$/</nowiki>


==UK Postcodes:==
==UK Postcodes:== <!--T:22-->


  <nowiki>/^[A-Z][A-Z]?[0-9][A-Z0-9]? ?[0-9][ABDEFGHJLNPQRSTUWXYZ]{2}$/i</nowiki>
  <!--T:23-->
<nowiki>/^[A-Z][A-Z]?[0-9][A-Z0-9]? ?[0-9][ABDEFGHJLNPQRSTUWXYZ]{2}$/i</nowiki>


<!--T:24-->
Note that this is not very exact, and a more exact validation is much more complex.  For example, see [http://stackoverflow.com/questions/164979/uk-postcode-regex-comprehensive StackOverflow answer] and [http://en.wikipedia.org/wiki/Talk:Postcodes_in_the_United_Kingdom#Regular_Expressions Wikipedia] for more information.
Note that this is not very exact, and a more exact validation is much more complex.  For example, see [http://stackoverflow.com/questions/164979/uk-postcode-regex-comprehensive StackOverflow answer] and [http://en.wikipedia.org/wiki/Talk:Postcodes_in_the_United_Kingdom#Regular_Expressions Wikipedia] for more information.


=Phone Numbers=
=Phone Numbers= <!--T:25-->


==US Phone Number:==
==US Phone Number:== <!--T:26-->


  <nowiki>/^(?:\([2-9]\d{2}\)\ ?|[2-9]\d{2}(?:\-?|\ ?))[2-9]\d{2}[- ]?\d{4}$/</nowiki>
  <!--T:27-->
<nowiki>/^(?:\([2-9]\d{2}\)\ ?|[2-9]\d{2}(?:\-?|\ ?))[2-9]\d{2}[- ]?\d{4}$/</nowiki>


<!--T:28-->
or
or


  <nowiki>/^[\(\)\.\- ]{0,}[0-9]{3}[\(\)\.\- ]{0,}[0-9]{3}[\(\)\.\- ]{0,}[0-9]{4}[\(\)\.\- ]{0,}$/</nowiki>
  <!--T:29-->
<nowiki>/^[\(\)\.\- ]{0,}[0-9]{3}[\(\)\.\- ]{0,}[0-9]{3}[\(\)\.\- ]{0,}[0-9]{4}[\(\)\.\- ]{0,}$/</nowiki>


<!--T:30-->
This second option will match all phone Canadian and US phone numbers that include non-digit symbols including
This second option will match all phone Canadian and US phone numbers that include non-digit symbols including


  <nowiki> . ( ) - (space)</nowiki>
  <!--T:31-->
<nowiki> . ( ) - (space)</nowiki>


<!--T:32-->
This will allow you to match phone numbers which resemble below.
This will allow you to match phone numbers which resemble below.
*(555)555 5555
*(555)555 5555
Line 71: Line 93:
*555555555
*555555555


==Australian Phone Number:==
==Australian Phone Number:== <!--T:33-->


<!--T:34-->
The following patterns match all various Australian mobile and landline phone numbers including with "+61" country prefix eg:
The following patterns match all various Australian mobile and landline phone numbers including with "+61" country prefix eg:
*(02) 9123 6535
*(02) 9123 6535
Line 79: Line 102:
*+61 2 3456 789
*+61 2 3456 789


<!--T:35-->
But not:
But not:
* 234 3450 234
* 234 3450 234
Line 85: Line 109:
*123456789013
*123456789013


<!--T:36-->
Brackets, white space and hypens are ignored.
Brackets, white space and hypens are ignored.


  <nowiki>NOTE: The 'PRECICE' versions listed here match against the first four or five didgets in a number to ensure that they are valid Australian numbers.
  <!--T:37-->
<nowiki>NOTE: The 'PRECICE' versions listed here match against the first four or five didgets in a number to ensure that they are valid Australian numbers.


<!--T:38-->
The 'NOT VERY PRECISE' only match against the first and second didgit so may allow invaid numbers</nowiki>
The 'NOT VERY PRECISE' only match against the first and second didgit so may allow invaid numbers</nowiki>


===All Australian phone numbers (mobile and landline - area code required)===
===All Australian phone numbers (mobile and landline - area code required)=== <!--T:39-->


  <nowiki>VERY PRECISE
  <!--T:40-->
<nowiki>VERY PRECISE


<!--T:41-->
/^\(?(?:\+?61|0)(?:(?:2\)?[ -]?(?:3[ -]?[38]|[46-9][ -]?[0-9]|5[ -]?[0-35-9])|3\)?(?:4[ -]?[0-57-9]|[57-9][ -]?[0-9]|6[ -]?[1-67])|7\)?[ -]?(?:[2-4][ -]?[0-9]|5[ -]?[2-7]|7[ -]?6)|8\)?[ -]?(?:5[ -]?[1-4]|6[ -]?[0-8]|[7-9][ -]?[0-9]))(?:[ -]?[0-9]){6}|4\)?[ -]?(?:(?:[01][ -]?[0-9]|2[ -]?[0-57-9]|3[ -]?[1-9]|4[ -]?[7-9]|5[ -]?[018])[ -]?[0-9]|3[ -]?0[ -]?[0-5])(?:[ -]?[0-9]){5})$/
/^\(?(?:\+?61|0)(?:(?:2\)?[ -]?(?:3[ -]?[38]|[46-9][ -]?[0-9]|5[ -]?[0-35-9])|3\)?(?:4[ -]?[0-57-9]|[57-9][ -]?[0-9]|6[ -]?[1-67])|7\)?[ -]?(?:[2-4][ -]?[0-9]|5[ -]?[2-7]|7[ -]?6)|8\)?[ -]?(?:5[ -]?[1-4]|6[ -]?[0-8]|[7-9][ -]?[0-9]))(?:[ -]?[0-9]){6}|4\)?[ -]?(?:(?:[01][ -]?[0-9]|2[ -]?[0-57-9]|3[ -]?[1-9]|4[ -]?[7-9]|5[ -]?[018])[ -]?[0-9]|3[ -]?0[ -]?[0-5])(?:[ -]?[0-9]){5})$/


<!--T:42-->
NOT VERY PRECISE
NOT VERY PRECISE


<!--T:43-->
/^(?:\+?61|0)[2-478](?:[ -]?[0-9]){8}$/</nowiki>
/^(?:\+?61|0)[2-478](?:[ -]?[0-9]){8}$/</nowiki>


===All Australian phone numbers (landlines only - area code required)===
===All Australian phone numbers (landlines only - area code required)=== <!--T:44-->


  <nowiki>VERY PRECISE
  <!--T:45-->
<nowiki>VERY PRECISE


<!--T:46-->
/^\(?(?:\+?61|0)(?:2\)?[ -]?(?:3[ -]?[38]|[46-9][ -]?[0-9]|5[ -]?[0-35-9])|3\)?(?:4[ -]?[0-57-9]|[57-9][ -]?[0-9]|6[ -]?[1-67])|7\)?[ -]?(?:[2-4][ -]?[0-9]|5[ -]?[2-7]|7[ -]?6)|8\)?[ -]?(?:5[ -]?[1-4]|6[ -]?[0-8]|[7-9][ -]?[0-9]))(?:[ -]?[0-9]){6}$/
/^\(?(?:\+?61|0)(?:2\)?[ -]?(?:3[ -]?[38]|[46-9][ -]?[0-9]|5[ -]?[0-35-9])|3\)?(?:4[ -]?[0-57-9]|[57-9][ -]?[0-9]|6[ -]?[1-67])|7\)?[ -]?(?:[2-4][ -]?[0-9]|5[ -]?[2-7]|7[ -]?6)|8\)?[ -]?(?:5[ -]?[1-4]|6[ -]?[0-8]|[7-9][ -]?[0-9]))(?:[ -]?[0-9]){6}$/


<!--T:47-->
NOT VERY PRECISE
NOT VERY PRECISE


<!--T:48-->
/^(?:\+?61|\(?0)[2378]\)?(?:[ -]?[0-9]){8}$/</nowiki>
/^(?:\+?61|\(?0)[2378]\)?(?:[ -]?[0-9]){8}$/</nowiki>


===New South Wales landline phone numbers (area code optional)===
===New South Wales landline phone numbers (area code optional)=== <!--T:49-->


  <nowiki>VERY PRECISE
  <!--T:50-->
<nowiki>VERY PRECISE


<!--T:51-->
/^(?:\(?(?:\+?61|0)2\)?[ -]?)?(?:3[ -]?[38]|[46-9][ -]?[0-9]|5[ -]?[0-35-9])(?:[ -]?[0-9]){6}$/
/^(?:\(?(?:\+?61|0)2\)?[ -]?)?(?:3[ -]?[38]|[46-9][ -]?[0-9]|5[ -]?[0-35-9])(?:[ -]?[0-9]){6}$/


<!--T:52-->
NOT VERY PRECISE
NOT VERY PRECISE


<!--T:53-->
/^(?:\(?(?:\+?61|0)2\)?(?:[ -]?[0-9]){7}[0-9]$/</nowiki>
/^(?:\(?(?:\+?61|0)2\)?(?:[ -]?[0-9]){7}[0-9]$/</nowiki>


===Victorian and Tasmanian landline phone numbers (area code optional)===
===Victorian and Tasmanian landline phone numbers (area code optional)=== <!--T:54-->


  <nowiki>VERY PRECISE
  <!--T:55-->
<nowiki>VERY PRECISE


<!--T:56-->
/^(?:\(?(?:\+?61|0)3\)?[ -]?)?(?:4[ -]?[0-57-9]|[57-9][ -]?[0-9]|6[ -]?[1-67])(?:[ -]?[0-9]){6}$/
/^(?:\(?(?:\+?61|0)3\)?[ -]?)?(?:4[ -]?[0-57-9]|[57-9][ -]?[0-9]|6[ -]?[1-67])(?:[ -]?[0-9]){6}$/


<!--T:57-->
NOT VERY PRECISE
NOT VERY PRECISE


<!--T:58-->
/^(?:\(?(?:\+?61|0)3\)?(?:[ -]?[0-9]){7}[0-9]$/</nowiki>
/^(?:\(?(?:\+?61|0)3\)?(?:[ -]?[0-9]){7}[0-9]$/</nowiki>


===Queensland landline phone numbers (area code optional)===
===Queensland landline phone numbers (area code optional)=== <!--T:59-->


  <nowiki>VERY PRECISE
  <!--T:60-->
<nowiki>VERY PRECISE


<!--T:61-->
/^(?:\(?(?:\+?61|0)7\)?[ -]?)?(?:[2-4][ -]?[0-9]|5[ -]?[2-7]|7[ -]?6)(?:[ -]?[0-9]){6}$/
/^(?:\(?(?:\+?61|0)7\)?[ -]?)?(?:[2-4][ -]?[0-9]|5[ -]?[2-7]|7[ -]?6)(?:[ -]?[0-9]){6}$/


<!--T:62-->
NOT VERY PRECISE
NOT VERY PRECISE


<!--T:63-->
/^(?:\(?(?:\+?61|0)7\)?(?:[ -]?[0-9]){7}[0-9]$/</nowiki>
/^(?:\(?(?:\+?61|0)7\)?(?:[ -]?[0-9]){7}[0-9]$/</nowiki>


===South Australia, Northern Territory, Western Australia landline phone numbers (area code optional)===
===South Australia, Northern Territory, Western Australia landline phone numbers (area code optional)=== <!--T:64-->


  <nowiki>VERY PRECISE
  <!--T:65-->
<nowiki>VERY PRECISE


<!--T:66-->
/^(?:\(?(?:\+?61|0)8\)?[ -]?)?(?:5[1-4]|6[0-8]|[7-9][0-9])$/
/^(?:\(?(?:\+?61|0)8\)?[ -]?)?(?:5[1-4]|6[0-8]|[7-9][0-9])$/


<!--T:67-->
NOT VERY PRECISE
NOT VERY PRECISE


<!--T:68-->
/^(?:\(?(?:\+?61|0)8\)?(?:[ -]?[0-9]){7}[0-9]$/</nowiki>
/^(?:\(?(?:\+?61|0)8\)?(?:[ -]?[0-9]){7}[0-9]$/</nowiki>


===Australian Mobile phone numbers only===
===Australian Mobile phone numbers only=== <!--T:69-->


  <nowiki>VERY PRECISE
  <!--T:70-->
<nowiki>VERY PRECISE


<!--T:71-->
/^(?:\+?61|0)4 ?(?:(?:[01] ?[0-9]|2 ?[0-57-9]|3 ?[1-9]|4 ?[7-9]|5 ?[018]) ?[0-9]|3 ?0 ?[0-5])(?: ?[0-9]){5}$/
/^(?:\+?61|0)4 ?(?:(?:[01] ?[0-9]|2 ?[0-57-9]|3 ?[1-9]|4 ?[7-9]|5 ?[018]) ?[0-9]|3 ?0 ?[0-5])(?: ?[0-9]){5}$/


<!--T:72-->
NOT VERY PRECISE
NOT VERY PRECISE


<!--T:73-->
/^(?:\(?(?:\+?61|0)4\)?(?:[ -]?[0-9]){7}[0-9]$/</nowiki>
/^(?:\(?(?:\+?61|0)4\)?(?:[ -]?[0-9]){7}[0-9]$/</nowiki>


==Belgian phone number==
==Belgian phone number== <!--T:74-->
  <nowiki>/^((\+|00)32\s?|0)(\d\s?\d{3}|\d{2}\s?\d{2})(\s?\d{2}){2}$/</nowiki>
  <nowiki>/^((\+|00)32\s?|0)(\d\s?\d{3}|\d{2}\s?\d{2})(\s?\d{2}){2}$/</nowiki>
==Belgian mobile phone number==
==Belgian mobile phone number==
Line 201: Line 256:
/^[1-9][0-9]{0,2}(?:,?[0-9]{3}){0,3}(?:\.[0-9]{2})?$/ same as above but the two decimal points are optional</nowiki>
/^[1-9][0-9]{0,2}(?:,?[0-9]{3}){0,3}(?:\.[0-9]{2})?$/ same as above but the two decimal points are optional</nowiki>


==Month (1-12)==
==Month (1-12)== <!--T:75-->


<!--T:76-->
If you want to ask for the month a person was born you can validate the input as follows:
If you want to ask for the month a person was born you can validate the input as follows:


  <nowiki>/^[0]*[1-9]$|^[0]*1[0-2]$/</nowiki>
  <!--T:77-->
<nowiki>/^[0]*[1-9]$|^[0]*1[0-2]$/</nowiki>


=Minimum width (set to 3 in this example)=
=Minimum width (set to 3 in this example)= <!--T:78-->


  <nowiki>/^.{3,}$/</nowiki>
  <!--T:79-->
<nowiki>/^.{3,}$/</nowiki>


=Currency=
=Currency= <!--T:80-->


==US currency (dollar sign and cents optional)==
==US currency (dollar sign and cents optional)== <!--T:81-->


  <nowiki>/^\$?\d+(\.(\d{2}]]?$/</nowiki>
  <!--T:82-->
<nowiki>/^\$?\d+(\.(\d{2}]]?$/</nowiki>


==Swiss price==
==Swiss price== <!--T:83-->


<!--T:84-->
A number with two decimal numbers after the decimal point of which the last one is either a 5 or a 0:
A number with two decimal numbers after the decimal point of which the last one is either a 5 or a 0:


  <nowiki>/^(\d+)(\.\d(05)?)?$/</nowiki>
  <!--T:85-->
<nowiki>/^(\d+)(\.\d(05)?)?$/</nowiki>


=Validate score=
=Validate score= <!--T:86-->


==1-10==
==1-10== <!--T:87-->


  <nowiki>/^[1-9]{1}$|^10$/</nowiki>
  <!--T:88-->
<nowiki>/^[1-9]{1}$|^10$/</nowiki>


==1-100==
==1-100== <!--T:89-->


  <nowiki>/^[1-9]?[0-9]{1}$|^100$/</nowiki>
  <!--T:90-->
<nowiki>/^[1-9]?[0-9]{1}$|^100$/</nowiki>


=Text validation=
=Text validation= <!--T:91-->


<!--T:92-->
currently multiple short text doesn't support minimum or maximum answers. One way around this is to use a long free text type question with a regular expression.
currently multiple short text doesn't support minimum or maximum answers. One way around this is to use a long free text type question with a regular expression.


<!--T:93-->
The following test for at least one word per line for at least 3 lines and no more than 10 lines.
The following test for at least one word per line for at least 3 lines and no more than 10 lines.


  <nowiki>/(?:[^,.;:?!& \n\r]+(?: [^,.;:?!& \n\r]+)*)(?:[,.;:?!& \n\r]?(?:\n|\r|\n\r|\r\n)(?:[^,.;:?!& \n\r]+(?: [^,.;:?!& \n\r]+)*)){2,10}/is</nowiki>
  <!--T:94-->
<nowiki>/(?:[^,.;:?!& \n\r]+(?: [^,.;:?!& \n\r]+)*)(?:[,.;:?!& \n\r]?(?:\n|\r|\n\r|\r\n)(?:[^,.;:?!& \n\r]+(?: [^,.;:?!& \n\r]+)*)){2,10}/is</nowiki>


<!--T:95-->
If you wanted, say five words per line you could change the first and last star/asterisk to {4,} e.g.
If you wanted, say five words per line you could change the first and last star/asterisk to {4,} e.g.


  <nowiki>/(?:[^,.;:?!& \n\r]+(?: [^,.;:?!& \n\r]+){4,})(?:[,.;:?!& \n\r]?(?:\n|\r|\n\r|\r\n)(?:[^,.;:?!& \n\r]+(?: [^,.;:?!& \n\r]+){4,})){2,10}/is</nowiki>
  <!--T:96-->
<nowiki>/(?:[^,.;:?!& \n\r]+(?: [^,.;:?!& \n\r]+){4,})(?:[,.;:?!& \n\r]?(?:\n|\r|\n\r|\r\n)(?:[^,.;:?!& \n\r]+(?: [^,.;:?!& \n\r]+){4,})){2,10}/is</nowiki>


<!--T:97-->
If you wanted one or more words per line on between 1 and 5 lines, you can change the content of the last curley braces to {0,4} (note you use 0 because you're already matching the first line.
If you wanted one or more words per line on between 1 and 5 lines, you can change the content of the last curley braces to {0,4} (note you use 0 because you're already matching the first line.


  <nowiki>/(?:[^,.;:?!& \n\r]+(?: [^,.;:?!& \n\r]+)*)(?:[,.;:?!& \n\r]?(?:\n|\r|\n\r|\r\n)(?:[^,.;:?!& \n\r]+(?: [^,.;:?!& \n\r]+)*)){0,4}/is</nowiki>
  <!--T:98-->
<nowiki>/(?:[^,.;:?!& \n\r]+(?: [^,.;:?!& \n\r]+)*)(?:[,.;:?!& \n\r]?(?:\n|\r|\n\r|\r\n)(?:[^,.;:?!& \n\r]+(?: [^,.;:?!& \n\r]+)*)){0,4}/is</nowiki>


==Word count==
==Word count== <!--T:99-->


  <nowiki>The following restricts the number of words allowed to a minimum of 1 and a maximum of 200
  <!--T:100-->
<nowiki>The following restricts the number of words allowed to a minimum of 1 and a maximum of 200


<!--T:101-->
/^[-\w]+(?:\W+[-\w]+){0,199}\W*$/</nowiki>
/^[-\w]+(?:\W+[-\w]+){0,199}\W*$/</nowiki>


  <nowiki>To increase the minimum change the zero part of {0,199}
  <!--T:102-->
<nowiki>To increase the minimum change the zero part of {0,199}


<!--T:103-->
To increase or decrease the maximum change the "199" part of {0,199}</nowiki>
To increase or decrease the maximum change the "199" part of {0,199}</nowiki>


=Time validation=
=Time validation= <!--T:104-->


<!--T:105-->
There are a number of ways of writing time formats. Some of the possible options are 12 hour or 24 hour, with seconds or without. Currently  (2009/04/06) LimeSurvey (v1.80plus) doesn't have a Time queston type so instead you can use "short free text" with one of the nine validation regular expressions below:
There are a number of ways of writing time formats. Some of the possible options are 12 hour or 24 hour, with seconds or without. Currently  (2009/04/06) LimeSurvey (v1.80plus) doesn't have a Time queston type so instead you can use "short free text" with one of the nine validation regular expressions below:


  <nowiki>The following three validation strings test for 24 hour time (in order of appearences) without seconds, with optional seconds lastly with seconds required.
  <!--T:106-->
<nowiki>The following three validation strings test for 24 hour time (in order of appearences) without seconds, with optional seconds lastly with seconds required.


<!--T:107-->
/^(?:[01][0-9]|2[0-3]):[0-5][0-9]$/
/^(?:[01][0-9]|2[0-3]):[0-5][0-9]$/


<!--T:108-->
/^(?:[01][0-9]|2[0-3]):[0-5][0-9](?::[0-5][0-9])?$/
/^(?:[01][0-9]|2[0-3]):[0-5][0-9](?::[0-5][0-9])?$/


<!--T:109-->
/^(?:[01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$/~/pp<span style="color:pp~The following three match 12 hour time, as above with seconds, optional seconds and with seconds required
/^(?:[01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$/~/pp<span style="color:pp~The following three match 12 hour time, as above with seconds, optional seconds and with seconds required


<!--T:110-->
/^(?">00:[0-5][0-9] (?:am|AM)|(?:0[1-9]|1[01]):[0-5][0-9] (?:[ap]m|[AP]M)|12:[0-5][0-9] (?:pm|PM))$/
/^(?">00:[0-5][0-9] (?:am|AM)|(?:0[1-9]|1[01]):[0-5][0-9] (?:[ap]m|[AP]M)|12:[0-5][0-9] (?:pm|PM))$/


<!--T:111-->
/^(?:00:[0-5][0-9](?::[0-5][0-9])? (?:am|AM)|(?:0[1-9]|1[01]):[0-5][0-9](?::[0-5][0-9])? (?:[ap]m|[AP]M)|12:[0-5][0-9](?::[0-5][0-9])? (?:pm|PM))$/
/^(?:00:[0-5][0-9](?::[0-5][0-9])? (?:am|AM)|(?:0[1-9]|1[01]):[0-5][0-9](?::[0-5][0-9])? (?:[ap]m|[AP]M)|12:[0-5][0-9](?::[0-5][0-9])? (?:pm|PM))$/


<!--T:112-->
/^(?:00:[0-5][0-9]:[0-5][0-9] (?:am|AM)|(?:0[1-9]|1[01]):[0-5][0-9]:[0-5][0-9] (?:[ap]m|[AP]M)|12:[0-5][0-9]:[0-5][0-9] (?:pm|PM))$/~/pp</span>pp~The following three match either 12 or 24 hour time as above with seconds, optional seconds and with seconds required
/^(?:00:[0-5][0-9]:[0-5][0-9] (?:am|AM)|(?:0[1-9]|1[01]):[0-5][0-9]:[0-5][0-9] (?:[ap]m|[AP]M)|12:[0-5][0-9]:[0-5][0-9] (?:pm|PM))$/~/pp</span>pp~The following three match either 12 or 24 hour time as above with seconds, optional seconds and with seconds required


<!--T:113-->
/^(?:(?:00:[0-5][0-9] (?:am|AM)|(?:0[1-9]|1[01]):[0-5][0-9] (?:[ap]m|[AP]M)|12:[0-5][0-9] (?:pm|PM))|(?:[01][0-9]|2[0-3]):[0-5][0-9])$/
/^(?:(?:00:[0-5][0-9] (?:am|AM)|(?:0[1-9]|1[01]):[0-5][0-9] (?:[ap]m|[AP]M)|12:[0-5][0-9] (?:pm|PM))|(?:[01][0-9]|2[0-3]):[0-5][0-9])$/


<!--T:114-->
/^(?:(?:00:[0-5][0-9](?<center>[0-5][0-9])? (?:am|AM)|(?:0[1-9]|1[01]):[0-5][0-9](?</center>[0-5][0-9])? (?:[ap]m|[AP]M)|12:[0-5][0-9](?<center>[0-5][0-9])? (?:pm|PM))|(?:[01][0-9]|2[0-3]):[0-5][0-9](?</center>[0-5][0-9])?)$/
/^(?:(?:00:[0-5][0-9](?<center>[0-5][0-9])? (?:am|AM)|(?:0[1-9]|1[01]):[0-5][0-9](?</center>[0-5][0-9])? (?:[ap]m|[AP]M)|12:[0-5][0-9](?<center>[0-5][0-9])? (?:pm|PM))|(?:[01][0-9]|2[0-3]):[0-5][0-9](?</center>[0-5][0-9])?)$/


<!--T:115-->
/^(?:(?:00:[0-5][0-9]:[0-5][0-9] (?:am|AM)|(?:0[1-9]|1[01]):[0-5][0-9]:[0-5][0-9] (?:[ap]m|[AP]M)|12:[0-5][0-9]:[0-5][0-9] (?:pm|PM))|(?:[01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9])$/</nowiki>
/^(?:(?:00:[0-5][0-9]:[0-5][0-9] (?:am|AM)|(?:0[1-9]|1[01]):[0-5][0-9]:[0-5][0-9] (?:[ap]m|[AP]M)|12:[0-5][0-9]:[0-5][0-9] (?:pm|PM))|(?:[01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9])$/</nowiki>


=US States=
=US States= <!--T:116-->


<!--T:117-->
To validate for one state use the following (example is Texas):
To validate for one state use the following (example is Texas):
*TX uppercase only = <nowiki>/^(TX)$/</nowiki>
*TX uppercase only = <nowiki>/^(TX)$/</nowiki>
Line 290: Line 376:
*TX upper or lowercase = <nowiki>/^([T|t][X|x])$/</nowiki>
*TX upper or lowercase = <nowiki>/^([T|t][X|x])$/</nowiki>


=Profanity Filter=
=Profanity Filter= <!--T:118-->


<!--T:119-->
To filter profanity words from an answer:
To filter profanity words from an answer:


  <nowiki>/^(?i)((?!\bENTERPROFANITYHERE\b).)*$(?-i)/</nowiki>
  <!--T:120-->
<nowiki>/^(?i)((?!\bENTERPROFANITYHERE\b).)*$(?-i)/</nowiki>


<!--T:121-->
Replace "ENTERPROFANITYHERE" with your bad word.
Replace "ENTERPROFANITYHERE" with your bad word.


<!--T:122-->
The \b will allow passing of words such as "assassination" & "hello" if you enter "ass" or "hell" as your profanity word. This also works if you are trying to omit other words, names etc. from answers.
The \b will allow passing of words such as "assassination" & "hello" if you enter "ass" or "hell" as your profanity word. This also works if you are trying to omit other words, names etc. from answers.


</translate>
</translate>

Revision as of 00:47, 9 April 2013

(Perl) Regular Expressions must start and finish with a forward slash ("/"). You can find a good library of regular expressions at http://www.regxlib.net/. These patterns will almost all work if surrounded with the forward slash.

To test your regex you can use this regex tester.

Please add successfully tested regular expressions!

Examples (note that these are all one line):

SPECIAL NOTE: Regular Expressions in Conditions

Note that when using regular expressions in the condition editor, do NOT include the beginning and ending slash.

Email Validation:

/(\w[-._\w]*\w@\w[-._\w]*\w\.\w{2,3})/

The above regex will not validate email adresses with "+" - which is valid, and e.g. promoted by gmail. The below regex is slightly modified and allows "+":

/^(\w[-._+\w]*\w@\w[-._\w]*\w\.\w{2,3})$/

It is still far from perfect - but I think its better then the original (also because start-of-string and end-of-string anchors are added). Mind. This will allow e.g. abc+++defg@somemail.office.com. Ideally the regex should be modified to only allow one "+" charachter.

Postcodes:

Australian Postcodes:

/^[0-9]{4}/

Brazilian Postcodes:

/^[0-9]{2}\.[0-9]{3}-[0-9]{3}$/

Canadian Postcodes:

/^[a-zA-Z]\d{1}[a-zA-Z](\-| |)\d{1}[a-zA-Z]\d{1}$/

US Postal Codes:

/^[0-9]{5}([- /]?[0-9]{4})?$/

UK Postcodes:

/^[A-Z][A-Z]?[0-9][A-Z0-9]? ?[0-9][ABDEFGHJLNPQRSTUWXYZ]{2}$/i

Note that this is not very exact, and a more exact validation is much more complex.  For example, see StackOverflow answer and Wikipedia for more information.

Phone Numbers

US Phone Number:

/^(?:\([2-9]\d{2}\)\ ?|[2-9]\d{2}(?:\-?|\ ?))[2-9]\d{2}[- ]?\d{4}$/

or

/^[\(\)\.\- ]{0,}[0-9]{3}[\(\)\.\- ]{0,}[0-9]{3}[\(\)\.\- ]{0,}[0-9]{4}[\(\)\.\- ]{0,}$/

This second option will match all phone Canadian and US phone numbers that include non-digit symbols including

 . ( ) - (space)

This will allow you to match phone numbers which resemble below.

  • (555)555 5555
  • 555.555.5555
  • 555 555 5555
  • (555)-555-5555
  • 555-555-5555
  • 555555555

Australian Phone Number:

The following patterns match all various Australian mobile and landline phone numbers including with "+61" country prefix eg:

  • (02) 9123 6535
  • 03 1234-5345
  • 0412 345 678
  • +61 2 3456 789

But not:

  • 234 3450 234
  • a234 534 3432
  • 134567
  • 123456789013

Brackets, white space and hypens are ignored.

NOTE: The 'PRECICE' versions listed here match against the first four or five didgets in a number to ensure that they are valid Australian numbers.

The 'NOT VERY PRECISE' only match against the first and second didgit so may allow invaid numbers

All Australian phone numbers (mobile and landline - area code required)

VERY PRECISE

/^\(?(?:\+?61|0)(?:(?:2\)?[ -]?(?:3[ -]?[38]|[46-9][ -]?[0-9]|5[ -]?[0-35-9])|3\)?(?:4[ -]?[0-57-9]|[57-9][ -]?[0-9]|6[ -]?[1-67])|7\)?[ -]?(?:[2-4][ -]?[0-9]|5[ -]?[2-7]|7[ -]?6)|8\)?[ -]?(?:5[ -]?[1-4]|6[ -]?[0-8]|[7-9][ -]?[0-9]))(?:[ -]?[0-9]){6}|4\)?[ -]?(?:(?:[01][ -]?[0-9]|2[ -]?[0-57-9]|3[ -]?[1-9]|4[ -]?[7-9]|5[ -]?[018])[ -]?[0-9]|3[ -]?0[ -]?[0-5])(?:[ -]?[0-9]){5})$/

NOT VERY PRECISE

/^(?:\+?61|0)[2-478](?:[ -]?[0-9]){8}$/

All Australian phone numbers (landlines only - area code required)

VERY PRECISE

/^\(?(?:\+?61|0)(?:2\)?[ -]?(?:3[ -]?[38]|[46-9][ -]?[0-9]|5[ -]?[0-35-9])|3\)?(?:4[ -]?[0-57-9]|[57-9][ -]?[0-9]|6[ -]?[1-67])|7\)?[ -]?(?:[2-4][ -]?[0-9]|5[ -]?[2-7]|7[ -]?6)|8\)?[ -]?(?:5[ -]?[1-4]|6[ -]?[0-8]|[7-9][ -]?[0-9]))(?:[ -]?[0-9]){6}$/

NOT VERY PRECISE

/^(?:\+?61|\(?0)[2378]\)?(?:[ -]?[0-9]){8}$/

New South Wales landline phone numbers (area code optional)

VERY PRECISE

/^(?:\(?(?:\+?61|0)2\)?[ -]?)?(?:3[ -]?[38]|[46-9][ -]?[0-9]|5[ -]?[0-35-9])(?:[ -]?[0-9]){6}$/

NOT VERY PRECISE

/^(?:\(?(?:\+?61|0)2\)?(?:[ -]?[0-9]){7}[0-9]$/

Victorian and Tasmanian landline phone numbers (area code optional)

VERY PRECISE

/^(?:\(?(?:\+?61|0)3\)?[ -]?)?(?:4[ -]?[0-57-9]|[57-9][ -]?[0-9]|6[ -]?[1-67])(?:[ -]?[0-9]){6}$/

NOT VERY PRECISE

/^(?:\(?(?:\+?61|0)3\)?(?:[ -]?[0-9]){7}[0-9]$/

Queensland landline phone numbers (area code optional)

VERY PRECISE

/^(?:\(?(?:\+?61|0)7\)?[ -]?)?(?:[2-4][ -]?[0-9]|5[ -]?[2-7]|7[ -]?6)(?:[ -]?[0-9]){6}$/

NOT VERY PRECISE

/^(?:\(?(?:\+?61|0)7\)?(?:[ -]?[0-9]){7}[0-9]$/

South Australia, Northern Territory, Western Australia landline phone numbers (area code optional)

VERY PRECISE

/^(?:\(?(?:\+?61|0)8\)?[ -]?)?(?:5[1-4]|6[0-8]|[7-9][0-9])$/

NOT VERY PRECISE

/^(?:\(?(?:\+?61|0)8\)?(?:[ -]?[0-9]){7}[0-9]$/

Australian Mobile phone numbers only

VERY PRECISE

/^(?:\+?61|0)4 ?(?:(?:[01] ?[0-9]|2 ?[0-57-9]|3 ?[1-9]|4 ?[7-9]|5 ?[018]) ?[0-9]|3 ?0 ?[0-5])(?: ?[0-9]){5}$/

NOT VERY PRECISE

/^(?:\(?(?:\+?61|0)4\)?(?:[ -]?[0-9]){7}[0-9]$/

Belgian phone number

/^((\+|00)32\s?|0)(\d\s?\d{3}|\d{2}\s?\d{2})(\s?\d{2}){2}$/

Belgian mobile phone number

/^((\+|00)32\s?|0)4(60|[789]\d)(\s?\d{2}){3}$/

French phone number

/^((\+|00)33\s?|0)[1-5](\s?\d{2}){4}$/

French mobile phone number

/^((\+|00)33\s?|0)[679](\s?\d{2}){4}$/

Luxemburg phone number

/^((\+|00\s?)352)?(\s?\d{2}){3,4}$/

Luxemburg mobile phone number

/^((\+|00\s?)352)?\s?6[269]1(\s?\d{3}){2}$/

German marks (with optional plus or minus)

/^[1-6]{1}[\+|\-]?$/

Age Validation

Example: Age 20-99

/([2-9][0-9])/

Example: Age 18-35

/(1[8-9]|2[0-9]|3[0-5])/

Example: Age 19-65

^(1[8-9]|[2-5][0-9]|6[0-5])$

Number validation

Numbers from 1 to 99999

/^([1-9]|[1-9][0-9]|[1-9][0-9][0-9]|[1-9][0-9][0-9][0-9]|[1-9][0-9][0-9][0-9][0-9])$/
/^[1-9][0-9]{0,4}$/ does the same as above but should run a little faster

Numbers from 1 to 999, 1.000 to 999.999 to 999.999.999

/^[1-9][0-9]{0,2}(?:\.[0-9]{3}){0,2}$/
accepts numbers from 1 to 999, 1.000 to 999.999 to 999.999.999 but
rejects numbers like 999.1.1 , 94.22.22, 999.1.22, 999.11.1, 999.1.333

Number validation with optionnal decimal (for price)

/^([1][0-9][0-9]|[1-9][0-9]|[0-9])((\.)[0-9][0-9])?$/ accepts numbers from 0 to 199, with 2 decimal optionnal

/^[1-9][0-9]{0,2}(?:,?[0-9]{3}){0,3}\.[0-9]{2}$/
forces two decimal points
accepts numbers from 1.00 to 999,999,999.00 with an optional comma delimiting thousands/millions
including all of the following: 1.00, 1,000.00 , 12,345.67 , 12345,02 , 123,456,468.00 , 1234566.00 , 123456789.00
but not 1,23.00 , 12,3.4 or 1234,43.04

/^[1-9][0-9]{0,2}(?:,?[0-9]{3}){0,3}(?:\.[0-9]{2})?$/ same as above but the two decimal points are optional

Month (1-12)

If you want to ask for the month a person was born you can validate the input as follows:

/^[0]*[1-9]$|^[0]*1[0-2]$/

Minimum width (set to 3 in this example)

/^.{3,}$/

Currency

US currency (dollar sign and cents optional)

/^\$?\d+(\.(\d{2}]]?$/

Swiss price

A number with two decimal numbers after the decimal point of which the last one is either a 5 or a 0:

/^(\d+)(\.\d(05)?)?$/

Validate score

1-10

/^[1-9]{1}$|^10$/

1-100

/^[1-9]?[0-9]{1}$|^100$/

Text validation

currently multiple short text doesn't support minimum or maximum answers. One way around this is to use a long free text type question with a regular expression.

The following test for at least one word per line for at least 3 lines and no more than 10 lines.

/(?:[^,.;:?!& \n\r]+(?: [^,.;:?!& \n\r]+)*)(?:[,.;:?!& \n\r]?(?:\n|\r|\n\r|\r\n)(?:[^,.;:?!& \n\r]+(?: [^,.;:?!& \n\r]+)*)){2,10}/is

If you wanted, say five words per line you could change the first and last star/asterisk to {4,} e.g.

/(?:[^,.;:?!& \n\r]+(?: [^,.;:?!& \n\r]+){4,})(?:[,.;:?!& \n\r]?(?:\n|\r|\n\r|\r\n)(?:[^,.;:?!& \n\r]+(?: [^,.;:?!& \n\r]+){4,})){2,10}/is

If you wanted one or more words per line on between 1 and 5 lines, you can change the content of the last curley braces to {0,4} (note you use 0 because you're already matching the first line.

/(?:[^,.;:?!& \n\r]+(?: [^,.;:?!& \n\r]+)*)(?:[,.;:?!& \n\r]?(?:\n|\r|\n\r|\r\n)(?:[^,.;:?!& \n\r]+(?: [^,.;:?!& \n\r]+)*)){0,4}/is

Word count

The following restricts the number of words allowed to a minimum of 1 and a maximum of 200

/^[-\w]+(?:\W+[-\w]+){0,199}\W*$/
To increase the minimum change the zero part of {0,199}

To increase or decrease the maximum change the "199" part of {0,199}

Time validation

There are a number of ways of writing time formats. Some of the possible options are 12 hour or 24 hour, with seconds or without. Currently  (2009/04/06) LimeSurvey (v1.80plus) doesn't have a Time queston type so instead you can use "short free text" with one of the nine validation regular expressions below:

The following three validation strings test for 24 hour time (in order of appearences) without seconds, with optional seconds lastly with seconds required.

/^(?:[01][0-9]|2[0-3]):[0-5][0-9]$/

/^(?:[01][0-9]|2[0-3]):[0-5][0-9](?::[0-5][0-9])?$/

/^(?:[01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$/~/pp<span style="color:pp~The following three match 12 hour time, as above with seconds, optional seconds and with seconds required

/^(?">00:[0-5][0-9] (?:am|AM)|(?:0[1-9]|1[01]):[0-5][0-9] (?:[ap]m|[AP]M)|12:[0-5][0-9] (?:pm|PM))$/

/^(?:00:[0-5][0-9](?::[0-5][0-9])? (?:am|AM)|(?:0[1-9]|1[01]):[0-5][0-9](?::[0-5][0-9])? (?:[ap]m|[AP]M)|12:[0-5][0-9](?::[0-5][0-9])? (?:pm|PM))$/

/^(?:00:[0-5][0-9]:[0-5][0-9] (?:am|AM)|(?:0[1-9]|1[01]):[0-5][0-9]:[0-5][0-9] (?:[ap]m|[AP]M)|12:[0-5][0-9]:[0-5][0-9] (?:pm|PM))$/~/pp</span>pp~The following three match either 12 or 24 hour time as above with seconds, optional seconds and with seconds required

/^(?:(?:00:[0-5][0-9] (?:am|AM)|(?:0[1-9]|1[01]):[0-5][0-9] (?:[ap]m|[AP]M)|12:[0-5][0-9] (?:pm|PM))|(?:[01][0-9]|2[0-3]):[0-5][0-9])$/

/^(?:(?:00:[0-5][0-9](?<center>[0-5][0-9])? (?:am|AM)|(?:0[1-9]|1[01]):[0-5][0-9](?</center>[0-5][0-9])? (?:[ap]m|[AP]M)|12:[0-5][0-9](?<center>[0-5][0-9])? (?:pm|PM))|(?:[01][0-9]|2[0-3]):[0-5][0-9](?</center>[0-5][0-9])?)$/

/^(?:(?:00:[0-5][0-9]:[0-5][0-9] (?:am|AM)|(?:0[1-9]|1[01]):[0-5][0-9]:[0-5][0-9] (?:[ap]m|[AP]M)|12:[0-5][0-9]:[0-5][0-9] (?:pm|PM))|(?:[01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9])$/

US States

To validate for one state use the following (example is Texas):

  • TX uppercase only = /^(TX)$/
  • tx lowercase only = /^(tx)$/
  • TX upper or lowercase = /^([T|t][X|x])$/

Profanity Filter

To filter profanity words from an answer:

/^(?i)((?!\bENTERPROFANITYHERE\b).)*$(?-i)/

Replace "ENTERPROFANITYHERE" with your bad word.

The \b will allow passing of words such as "assassination" & "hello" if you enter "ass" or "hell" as your profanity word. This also works if you are trying to omit other words, names etc. from answers.