Actions

Using regular expressions: Difference between revisions

From LimeSurvey Manual

(Marked this version for translation)
m (error syntaxhighlighting)
Line 24: Line 24:


<!--T:123-->
<!--T:123-->
<source>
<source lang="html">
/^(\w[-._+\w]*\w@\w[-._\w]*\w\.\w{2,3})$/
/^(\w[-._+\w]*\w@\w[-._\w]*\w\.\w{2,3})$/
</source>
</source>
Line 33: Line 33:


  <!--T:15-->
  <!--T:15-->
<source>/^[0-9]{4}/</source>
<source lang="html">/^[0-9]{4}/</source>


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


  <!--T:17-->
  <!--T:17-->
<source>/^[0-9]{2}\.[0-9]{3}-[0-9]{3}$/</source>
<source lang="html">/^[0-9]{2}\.[0-9]{3}-[0-9]{3}$/</source>


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


  <!--T:19-->
  <!--T:19-->
<source>/^[a-zA-Z]\d{1}[a-zA-Z](\-| |)\d{1}[a-zA-Z]\d{1}$/</source>
<source lang="html">/^[a-zA-Z]\d{1}[a-zA-Z](\-| |)\d{1}[a-zA-Z]\d{1}$/</source>


==US postal codes== <!--T:20-->
==US postal codes== <!--T:20-->


  <!--T:21-->
  <!--T:21-->
<source>/^[0-9]{5}([- /]?[0-9]{4})?$/</source>
<source lang="html">/^[0-9]{5}([- /]?[0-9]{4})?$/</source>


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


  <!--T:23-->
  <!--T:23-->
<source>/^[A-Z][A-Z]?[0-9][A-Z0-9]? ?[0-9][ABDEFGHJLNPQRSTUWXYZ]{2}$/i</source>
<source lang="html">/^[A-Z][A-Z]?[0-9][A-Z0-9]? ?[0-9][ABDEFGHJLNPQRSTUWXYZ]{2}$/i</source>


<!--T:24-->
<!--T:24-->
Line 63: Line 63:


  <!--T:27-->
  <!--T:27-->
<source>/^(?:\([2-9]\d{2}\)\ ?|[2-9]\d{2}(?:\-?|\ ?))[2-9]\d{2}[- ]?\d{4}$/</source>
<source lang="html">/^(?:\([2-9]\d{2}\)\ ?|[2-9]\d{2}(?:\-?|\ ?))[2-9]\d{2}[- ]?\d{4}$/</source>


<!--T:28-->
<!--T:28-->
Line 69: Line 69:


  <!--T:29-->
  <!--T:29-->
<source>/^[\(\)\.\- ]{0,}[0-9]{3}[\(\)\.\- ]{0,}[0-9]{3}[\(\)\.\- ]{0,}[0-9]{4}[\(\)\.\- ]{0,}$/</source>
<source lang="html">/^[\(\)\.\- ]{0,}[0-9]{3}[\(\)\.\- ]{0,}[0-9]{3}[\(\)\.\- ]{0,}[0-9]{4}[\(\)\.\- ]{0,}$/</source>


<!--T:30-->
<!--T:30-->
Line 75: Line 75:


  <!--T:31-->
  <!--T:31-->
<source> . ( ) - (space)</source>
<source lang="html"> . ( ) - (space)</source>


<!--T:32-->
<!--T:32-->
Line 117: Line 117:


<!--T:41-->
<!--T:41-->
<source>/^\(?(?:\+?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})$/
<source lang="html">/^\(?(?:\+?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})$/
</source>
</source>


Line 125: Line 125:


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


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


<!--T:46-->
<!--T:46-->
<source>/^\(?(?:\+?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}$/</source>
<source lang="html">/^\(?(?:\+?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}$/</source>




Line 140: Line 140:


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


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


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


<!--T:52-->
<!--T:52-->
Line 154: Line 154:


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


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


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


<!--T:57-->
<!--T:57-->
Line 168: Line 168:


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


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


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




Line 183: Line 183:


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


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


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


<!--T:67-->
<!--T:67-->
Line 197: Line 197:


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


===Australian mobile phone numbers only=== <!--T:69-->
===Australian mobile phone numbers only=== <!--T:69-->
Line 205: Line 205:


<!--T:71-->
<!--T:71-->
<source>/^(?:\+?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}$/</source>
<source lang="html">/^(?:\+?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}$/</source>




Line 212: Line 212:


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


==Belgian phone number== <!--T:74-->
==Belgian phone number== <!--T:74-->
  <source>/^((\+|00)32\s?|0)(\d\s?\d{3}|\d{2}\s?\d{2})(\s?\d{2}){2}$/</source>
  <source lang="html">/^((\+|00)32\s?|0)(\d\s?\d{3}|\d{2}\s?\d{2})(\s?\d{2}){2}$/</source>
==Belgian mobile phone number==
==Belgian mobile phone number==
  <source>/^((\+|00)32\s?|0)4(60|[789]\d)(\s?\d{2}){3}$/</source>
  <source>/^((\+|00)32\s?|0)4(60|[789]\d)(\s?\d{2}){3}$/</source>
==French phone number==
==French phone number==
  <source>/^((\+|00)33\s?|0)[1-5](\s?\d{2}){4}$/</source>
  <source lang="html">/^((\+|00)33\s?|0)[1-5](\s?\d{2}){4}$/</source>
==French mobile phone number==
==French mobile phone number==
  <source>/^((\+|00)33\s?|0)[679](\s?\d{2}){4}$/</source>
  <source lang="html">/^((\+|00)33\s?|0)[679](\s?\d{2}){4}$/</source>
==Luxemburg phone number==
==Luxemburg phone number==
  <source>/^((\+|00\s?)352)?(\s?\d{2}){3,4}$/</source>
  <source lang="html">/^((\+|00\s?)352)?(\s?\d{2}){3,4}$/</source>
==Luxemburg mobile phone number==
==Luxemburg mobile phone number==
  <source>/^((\+|00\s?)352)?\s?6[269]1(\s?\d{3}){2}$/</source>
  <source lang="html">/^((\+|00\s?)352)?\s?6[269]1(\s?\d{3}){2}$/</source>
=German marks (with optional plus or minus)=
=German marks (with optional plus or minus)=
  <source>/^[1-6]{1}[\+|\-]?$/</source>
  <source lang="html">/^[1-6]{1}[\+|\-]?$/</source>
=Age validation=
=Age validation=
Example: Age 20-99
Example: Age 20-99
  <source>/([2-9][0-9])/</source>
  <source lang="html">/([2-9][0-9])/</source>




<!--T:124-->
<!--T:124-->
Example: Age 18-35
Example: Age 18-35
  <source>/(1[8-9]|2[0-9]|3[0-5])/</source>
  <source lang="html">/(1[8-9]|2[0-9]|3[0-5])/</source>




<!--T:125-->
<!--T:125-->
Example: Age 19-65
Example: Age 19-65
  <source>/^(1[8-9]|[2-5][0-9]|6[0-5])$/</source>
  <source lang="html">/^(1[8-9]|[2-5][0-9]|6[0-5])$/</source>


=Number validation= <!--T:126-->
=Number validation= <!--T:126-->
==Numbers from 1 to 99999==
==Numbers from 1 to 99999==
  <source>/^([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])$/</source>
  <source lang="html">/^([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])$/</source>
  <source>/^[1-9][0-9]{0,4}$/ does the same as above but should run a little faster</source>
  <source lang="html">/^[1-9][0-9]{0,4}$/ does the same as above but should run a little faster</source>
==Numbers from 1 to 999, 1.000 to 999.999 to 999.999.999==
==Numbers from 1 to 999, 1.000 to 999.999 to 999.999.999==
  <source>/^[1-9][0-9]{0,2}(?:\.[0-9]{3}){0,2}$/</source>
  <source lang="html">/^[1-9][0-9]{0,2}(?:\.[0-9]{3}){0,2}$/</source>
Accepts numbers from 1 to 999, 1.000 to 999.999 to 999.999.999 but
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
rejects numbers like 999.1.1 , 94.22.22, 999.1.22, 999.11.1, 999.1.333
Line 253: Line 253:
==Number validation with optional decimal (for price)== <!--T:127-->
==Number validation with optional decimal (for price)== <!--T:127-->
Accepts numbers from 0 to 199, with 2 decimal optional:
Accepts numbers from 0 to 199, with 2 decimal optional:
  <source>/^([1][0-9][0-9]|[1-9][0-9]|[0-9])((\.)[0-9][0-9])?$/</source>
  <source lang="html">/^([1][0-9][0-9]|[1-9][0-9]|[0-9])((\.)[0-9][0-9])?$/</source>




Line 260: Line 260:
including all of the following: 1.00, 1,000.00, 12,345.67, 12345,02, 123,456,468.00, 1234566.00, 123456789.00
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
but not 1,23.00, 12,3.4 or 1234,43.04
  <source>/^[1-9][0-9]{0,2}(?:,?[0-9]{3}){0,3}\.[0-9]{2}$/</source>
  <source lang="html">/^[1-9][0-9]{0,2}(?:,?[0-9]{3}){0,3}\.[0-9]{2}$/</source>
   
   
Similar to the above: Forces two decimal points but accepts a "0" before decimal separator ",".
Similar to the above: Forces two decimal points but accepts a "0" before decimal separator ",".
  <source>/[0-9]{0,2}(?:,?[0-9]{3}){0,3}\.[0-9]{2}$/</source>
  <source lang="html">/[0-9]{0,2}(?:,?[0-9]{3}){0,3}\.[0-9]{2}$/</source>


<!--T:129-->
<!--T:129-->
Same as above but the two decimal points are optional:
Same as above but the two decimal points are optional:
<source>/^[1-9][0-9]{0,2}(?:,?[0-9]{3}){0,3}(?:\.[0-9]{2})?$/ </source>
<source lang="html">/^[1-9][0-9]{0,2}(?:,?[0-9]{3}){0,3}(?:\.[0-9]{2})?$/ </source>


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


  <!--T:77-->
  <!--T:77-->
<source>/^[0]*[1-9]$|^[0]*1[0-2]$/</source>
<source lang="html">/^[0]*[1-9]$|^[0]*1[0-2]$/</source>


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


  <!--T:79-->
  <!--T:79-->
<source>/^.{3,}$/</source>
<source lang="html">/^.{3,}$/</source>


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


  <!--T:82-->
  <!--T:82-->
<source>/^\$?\d+(\.(\d{2}))?$/</source>
<source lang="html">/^\$?\d+(\.(\d{2}))?$/</source>


<!--T:132-->
<!--T:132-->
Check for comma usage:
Check for comma usage:
<source>/^\$?\d{1,3}(\d+(?!,))?(,\d{3})*(\.\d{2})?$/</source>
<source lang="html">/^\$?\d{1,3}(\d+(?!,))?(,\d{3})*(\.\d{2})?$/</source>


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


  <!--T:85-->
  <!--T:85-->
<source>/^(\d+)(\.\d(05)?)?$/</source>
<source lang="html">/^(\d+)(\.\d(05)?)?$/</source>


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


  <!--T:88-->
  <!--T:88-->
<source>/^[1-9]{1}$|^10$/</source>
<source lang="html">/^[1-9]{1}$|^10$/</source>


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


  <!--T:90-->
  <!--T:90-->
<source>/^[1-9]?[0-9]{1}$|^100$/</source>
<source lang="html">/^[1-9]?[0-9]{1}$|^100$/</source>


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


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


<!--T:95-->
<!--T:95-->
Line 328: Line 328:


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


<!--T:97-->
<!--T:97-->
Line 334: Line 334:


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


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


  <!--T:100-->
  <!--T:100-->
<source>The following restricts the number of words allowed to a minimum of 1 and a maximum of 200
<source lang="html">The following restricts the number of words allowed to a minimum of 1 and a maximum of 200


<!--T:101-->
<!--T:101-->
Line 345: Line 345:


  <!--T:102-->
  <!--T:102-->
<source>To increase the minimum change the zero part of {0,199}
<source lang="html">To increase the minimum change the zero part of {0,199}


<!--T:103-->
<!--T:103-->
Line 359: Line 359:


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


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


<!--T:109-->
<!--T:109-->
<source>/^(?:[01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$/</source>
<source lang="html">/^(?:[01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$/</source>




Line 373: Line 373:


<!--T:110-->
<!--T:110-->
<source>/^(?">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))$/</source>
<source lang="html">/^(?">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))$/</source>


<!--T:111-->
<!--T:111-->
<source>/^(?: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))$/</source>
<source lang="html">/^(?: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))$/</source>


<!--T:112-->
<!--T:112-->
<source>/^(?: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))$/</source>
<source lang="html">/^(?: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))$/</source>




Line 387: Line 387:


<!--T:113-->
<!--T:113-->
<source>/^(?:(?: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])$/</source>
<source lang="html">/^(?:(?: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])$/</source>


<!--T:114-->
<!--T:114-->
<source>/^(?:(?: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])?)$/</source>
<source lang="html">/^(?:(?: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])?)$/</source>


<!--T:115-->
<!--T:115-->
<source>/^(?:(?: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])$/</source>
<source lang="html">/^(?:(?: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])$/</source>


=US states= <!--T:116-->
=US states= <!--T:116-->
Line 409: Line 409:


  <!--T:120-->
  <!--T:120-->
<source>/^(?i)((?!\bENTERPROFANITYHERE\b).)*$(?-i)/</source>
<source lang="html">/^(?i)((?!\bENTERPROFANITYHERE\b).)*$(?-i)/</source>


<!--T:121-->
<!--T:121-->

Revision as of 15:04, 26 February 2018

Regular Expressions must start and finish with a forward slash ("/"). You can find a good library of regular expressions at http://www.regexlib.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):

Important: 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})$/

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 numbers

/^(?:\([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 numbers

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.

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

The 'Not very precise:' only match against the first and second digit 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 optional decimal (for price)

Accepts numbers from 0 to 199, with 2 decimal optional:

/^([1][0-9][0-9]|[1-9][0-9]|[0-9])((\.)[0-9][0-9])?$/


Forces two decimal points and 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}$/

Similar to the above: Forces two decimal points but accepts a "0" before decimal separator ",".

/[0-9]{0,2}(?:,?[0-9]{3}){0,3}\.[0-9]{2}$/

Same as above but the two decimal points are optional:

/^[1-9][0-9]{0,2}(?:,?[0-9]{3}){0,3}(?:\.[0-9]{2})?$/

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}))?$/

Check for comma usage:

/^\$?\d{1,3}(\d+(?!,))?(,\d{3})*(\.\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. Although it is an option to use the date question type (it can also capture time) you can use "short free text" with one of the 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]$/


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))$/


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](?[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])?)$/
/^(?:(?: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.