Actions

Using regular expressions/de: Difference between revisions

From LimeSurvey Manual

(Updating to match new version of source page)
(Updating to match new version of source page)
Line 6: Line 6:
Um Ihre RegEx zu testen, können Sie [https://regex101.com/ diesen RegEx-Tester] verwenden.
Um Ihre RegEx zu testen, können Sie [https://regex101.com/ diesen RegEx-Tester] verwenden.


<div class="mw-translate-fuzzy">
"Bitte fügen Sie erfolgreich getestet reguläre Ausdrücke dieser Seite hinzu!"
"Bitte fügen Sie erfolgreich getestet reguläre Ausdrücke dieser Seite hinzu!"
</div>


<div class="mw-translate-fuzzy">
Beispiele (beachten Sie, dass dies alles Einzeiler sind):
Beispiele (beachten Sie, dass dies alles Einzeiler sind):
</div>


=Wichtig: Reguläre Ausdrücke in Bedingungen=
=Wichtig: Reguläre Ausdrücke in Bedingungen=


Beachten Sie, dass bei der Verwendung von regulären Ausdrücken im Bedingungseditor, der Anfangs-/Endschrägstrich NICHT benutzt werden darf.
Beachten Sie, dass bei der Verwendung von regulären Ausdrücken im Bedingungseditor, der Anfangs-/Endschrägstrich NICHT benutzt werden darf.


=E-Mail-Validierung=
=E-Mail-Validierung=


<source lang="html">
 
<div class="mw-translate-fuzzy">
<source>
/^(\w[-._+\w]*\w@\w[-._\w]*\w\.\w{2,3})$/
/^(\w[-._+\w]*\w@\w[-._\w]*\w\.\w{2,3})$/
</source>
</source>
</div>


=Postleitzahlen=
=Postleitzahlen=
Line 24: Line 34:
==Australische Postleitzahlen:==
==Australische Postleitzahlen:==


  <source lang="html">/^[0-9]{4}/</source>
  <div class="mw-translate-fuzzy">
<source>/^[0-9]{4}/</source>
</div>


==Brasilianische Postleitzahlen==
==Brasilianische Postleitzahlen==


  <source lang="html">/^[0-9]{2}\.[0-9]{3}-[0-9]{3}$/</source>
  <div class="mw-translate-fuzzy">
<source>/^[0-9]{2}\.[0-9]{3}-[0-9]{3}$/</source>
</div>


==Kanadische Postleitzahlen==
==Kanadische Postleitzahlen==


  <source lang="html">/^[a-zA-Z]\d{1}[a-zA-Z](\-| |)\d{1}[a-zA-Z]\d{1}$/</source>
  <div class="mw-translate-fuzzy">
<source>/^[a-zA-Z]\d{1}[a-zA-Z](\-| |)\d{1}[a-zA-Z]\d{1}$/</source>
</div>


==US Postleitzahlen==
==US Postleitzahlen==


  <source lang="html">/^[0-9]{5}([- /]?[0-9]{4})?$/</source>
  <div class="mw-translate-fuzzy">
<source>/^[0-9]{5}([- /]?[0-9]{4})?$/</source>
</div>


==UK Postleitzahlen==
==UK Postleitzahlen==


  <source lang="html">/^[A-Z][A-Z]?[0-9][A-Z0-9]? ?[0-9][ABDEFGHJLNPQRSTUWXYZ]{2}$/i</source>
  <div class="mw-translate-fuzzy">
<source>/^[A-Z][A-Z]?[0-9][A-Z0-9]? ?[0-9][ABDEFGHJLNPQRSTUWXYZ]{2}$/i</source>
</div>
 
Note that this is not very exact, and a more exact validation is much more complex. For example, see [https://stackoverflow.com/questions/164979/uk-postcode-regex-comprehensive StackOverflow answer] and [https://en.wikipedia.org/wiki/Talk:Postcodes_in_the_United_Kingdom#Regular_Expressions Wikipedia] for more information.
 
==French postcodes==
 
<source lang="html">/(^[0-8]\d\d{3}$)|(^9[0-5]\d{3}$)|(^97[1-6]\d{2}$)|(^98[46-8]\d{2}$)/</source>
 
The above expression is very precise and it will check whether or not the French Department exists (first two digits), including overseas Departments and overseas Territories (DOM-TOM).


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=


==US phone numbers==
==US phone numbers==


<source lang="html">/^(?:\([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>


or
or
Line 54: Line 82:
  <source lang="html">/^[\(\)\.\- ]{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>


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:


  <source lang="html"> . ( ) - (space)</source>
  <div class="mw-translate-fuzzy">
<source> . ( ) - (space)</source>
</div>


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
*555.555.5555
*555.555.5555
Line 66: Line 96:
*555555555
*555555555


==Australian phone numbers==
<div class="mw-translate-fuzzy">
==Australische Telefonnummern==
</div>


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:
*(02) 9123 6535
*(02) 9123 6535
*03 1234-5345
*03 1234-5345
Line 92: Line 124:
<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 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>


Not very precise:
Not very precise:
Line 103: Line 134:


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


Not very precise:
Not very precise:
Line 134: Line 164:


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


Not very precise:
Not very precise:
Line 155: Line 184:


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


Not very precise:
Not very precise:
Line 166: Line 194:
  <source lang="html">/^((\+|00)32\s?|0)4(60|[789]\d)(\s?\d{2}){3}$/</source>
  <source lang="html">/^((\+|00)32\s?|0)4(60|[789]\d)(\s?\d{2}){3}$/</source>
==French phone number==
==French phone number==
  <source lang="html">/^((\+|00)33\s?|0)[1-5](\s?\d{2}){4}$/</source>
  <source lang="html">/^((\+|00)33\s?|0)[1-59](\s?\d{2}){4}$/</source>
==French mobile phone number==
==French mobile phone number==
  <source lang="html">/^((\+|00)33\s?|0)[679](\s?\d{2}){4}$/</source>
  <source lang="html">/^((\+|00)33\s?|0)[67](\s?\d{2}){4}$/</source>
==Luxemburg phone number==
==Luxemburg phone number==
  <source lang="html">/^((\+|00\s?)352)?(\s?\d{2}){3,4}$/</source>
  <source lang="html">/^((\+|00\s?)352)?(\s?\d{2}){3,4}$/</source>
Line 175: Line 203:
=German marks (with optional plus or minus)=
=German marks (with optional plus or minus)=
  <source lang="html">/^[1-6]{1}[\+|\-]?$/</source>
  <source lang="html">/^[1-6]{1}[\+|\-]?$/</source>
=Age validation=
=Age validation=
Example: Age 20-99
Example: Age 20-99
  <source lang="html">/([2-9][0-9])/</source>
  <source lang="html">/([2-9][0-9])/</source>


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


Example: Age 19-65
Example: Age 19-65
  <source lang="html">/^(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=
 
=Number validation=  
 
 
==Numbers from 1 to 99999==
==Numbers from 1 to 99999==
  <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 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>
Line 200: Line 233:
  <source lang="html">/^([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>


 
It forces two decimal points and accepts numbers from 1.00 to 999,999,999.00 with an optional comma delimiting thousands/millions
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
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
Line 209: Line 241:
  <source lang="html">/[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>


Same as above but the two decimal points are optional:
Same as above, but the two decimal points are optional:
<source lang="html">/^[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>


Line 217: Line 249:


  <source lang="html">/^[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)=
=Minimum width (set to 3 in this example)=


  <source lang="html">/^.{3,}$/</source>
  <source lang="html">/^.{3,}$/</source>


=Currency=
=Currency=


==US currency (dollar sign and cents optional)==
==US currency (dollar sign and cents optional)==
Line 236: Line 272:


  <source lang="html">/^(\d+)(\.\d(05)?)?$/</source>
  <source lang="html">/^(\d+)(\.\d(05)?)?$/</source>


=Validate score=
=Validate score=


==1-10==
==1-10==
Line 246: Line 284:


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


=Text validation=
=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.
The [[Question type - Multiple short text|multiple short text question type]] doesn't support minimum or maximum answers at the moment. One way around this is to use a [[Question type - Long free text|long free text question type]] with a regular expression.
 
The below expression tests for at least one word per line for at least 3 lines and no more than 10 lines:


  <source lang="html">/(?:[^,.;:?!& \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>


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


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


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 (between line 1 and line 5), you can change the content located within the last curly braces to '''0,4''' (note you use 0 because you're already matching the first line).


  <source lang="html">/(?:[^,.;:?!& \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>
Line 265: Line 305:
==Word count==
==Word count==


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


<source lang="html">/^[-\w]+(?:\W+[-\w]+){0,199}\W*$/</source>
<source lang="html">/^[-\w]+(?:\W+[-\w]+){0,199}\W*$/</source>


  To increase the minimum change the zero part of {0,199}
  To increase the minimum change the zero part of {0,199}.


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


=Time validation=
=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.
There are a number of ways to write 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 [[Question type - Date|date question type]] (it can also capture time) you can use the [[Question type - Short free text|short free text question type]] with one of the below validation regular expressions.
 
The following three validation strings test for 24 hour time (in order of appearances) without seconds, with optional seconds lastly with seconds required:


<source lang="html">/^(?:[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>
Line 285: Line 326:
<source lang="html">/^(?:[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>


 
The following three match 12 hour time, as above with seconds, optional seconds and with seconds required:
 
The following three match 12 hour time, as above with seconds, optional seconds and with seconds required


<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>
<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>
Line 295: Line 334:
<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>
<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>


 
The following three match either 12 or 24 hour time as above with seconds, optional seconds and with seconds required:
 
The following three match either 12 or 24 hour time as above with seconds, optional seconds and with seconds required


<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>
<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>
Line 306: Line 343:


=US states=
=US states=


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 = <source lang="html">/^(TX)$/</source>
*TX uppercase only: <source lang="html">/^(TX)$/</source>
*tx lowercase only = <source lang="html">/^(tx)$/</source>
*tx lowercase only: <source lang="html">/^(tx)$/</source>
*TX upper or lowercase = <source lang="html">/^([T|t][X|x])$/</source>
*TX upper or lowercase: <source lang="html">/^([T|t][X|x])$/</source>
 


=Profanity Filter=
=Profanity Filter=


To filter profanity words from an answer:
To filter profanity words from an answer:
Line 320: Line 360:
Replace "ENTERPROFANITYHERE" with your bad word.
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.
The \b will allow passing of words such as "assassination" and "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.
 
 
=Helpful links=
 
 
In the beginning of this wiki section, we recommend you to use https://regex101.com/ to test/create regular expressions. You can also use https://www.regextester.com/ to create expressions in case you are unhappy about the first option.

Revision as of 15:45, 8 January 2019

Reguläre Ausdrücke müssen am Anfang und Ende mit einem Schrägstrich ("/") umschlossen sein. Sie finden eine gute Bibliothek mit regulären Ausdrücken auf http://www.regexlib.net/. Diese Ausdrücke werden fast alle funktionieren, wenn Sie diese mit dem Schrägstrich einschließen.

Um Ihre RegEx zu testen, können Sie diesen RegEx-Tester verwenden.

"Bitte fügen Sie erfolgreich getestet reguläre Ausdrücke dieser Seite hinzu!"

Beispiele (beachten Sie, dass dies alles Einzeiler sind):


Wichtig: Reguläre Ausdrücke in Bedingungen

Beachten Sie, dass bei der Verwendung von regulären Ausdrücken im Bedingungseditor, der Anfangs-/Endschrägstrich NICHT benutzt werden darf.


E-Mail-Validierung

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

Postleitzahlen

Australische Postleitzahlen:

/^[0-9]{4}/

Brasilianische Postleitzahlen

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

Kanadische Postleitzahlen

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

US Postleitzahlen

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

UK Postleitzahlen

/^[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.

French postcodes

/(^[0-8]\d\d{3}$)|(^9[0-5]\d{3}$)|(^97[1-6]\d{2}$)|(^98[46-8]\d{2}$)/

The above expression is very precise and it will check whether or not the French Department exists (first two digits), including overseas Departments and overseas Territories (DOM-TOM).


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

Australische Telefonnummern

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

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

But not:

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

Klammern, Leerzeichen und Bindestriche werden ignoriert.

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-59](\s?\d{2}){4}$/

French mobile phone number

/^((\+|00)33\s?|0)[67](\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])?$/

It 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

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

The below expression tests 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 want, say five words per line, you could change the first and last star/asterisk to {4,}:

/(?:[^,.;:?!& \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 (between line 1 and line 5), you can change the content located within the last curly 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 to write 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 the short free text question type with one of the below validation regular expressions.

The following three validation strings test for 24 hour time (in order of appearances) 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" and "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.


Helpful links

In the beginning of this wiki section, we recommend you to use https://regex101.com/ to test/create regular expressions. You can also use https://www.regextester.com/ to create expressions in case you are unhappy about the first option.