Uporaba regularnih izrazov
From LimeSurvey Manual
Regularni izrazi se morajo začeti in končati s poševnico naprej ("/"). Dobro knjižnico regularnih izrazov najdete na http://www.regexlib.net/. Ti vzorci bodo skoraj vedno delovali, če so obkroženi s poševnicami.
Za testiranje svojega regularnega izraza lahko uporabite ta preizkuševalec regularnih izrazov.
Pomembno: Regularni izrazi v pogojih
Upoštevajte, da pri uporabi regularnih izrazov v urejevalniku pogojev NE vključite začetne in končne poševnice.
E-poštno preverjanje
Posodobitev (12/2018): Ker dandanes imena domen ne morejo vsebovati samo posebnih znakov, kot je »ü«, ampak uporabljajo tudi več kot 3 znake kot ime domene, kot je .tech ali .company, spodnji e-poštni regularni izraz preveri samo, če sta »@« in ».« obstaja znotraj vnesenega elektronskega naslova.
/^.+?@.+?\..+$/
Če želite potrditi en naslov na vrstico, v večvrstičnem dolgem besedilnem polju:
/^(((\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*(\r\ n)?\n?)+)*)$/
Pri tem zastarelem regularnem izrazu je ime domene omejeno na 3 znake, kar ne pokriva vseh razpoložljivih domen:
/^(\w[-._+\w]*\w@\w[-._\w]*\w\.\w{2,3})$/
Poštne številke
Avstralske poštne številke:
/^[0-9]{4}/
Brazilske poštne številke
/^[0-9]{2}\.[0-9]{3}-[0-9]{3}$/
Kanadske poštne številke
/^[a-zA-Z]\d{1}[a-zA-Z](\-| |)\d{1}[a-zA-Z]\d{1}$/
Poštne številke ZDA
/^[0-9]{5}([- /]?[0-9]{4})?$/
Če želite, da se poštna številka začne z ničlo, uporabite:
/^[0-9]{5}(?:-[0-9]{4})?$/
Poštne številke Združenega kraljestva
/^[AZ][AZ]?[0-9][A-Z0-9]? ?[0-9][ABDEFGHJLNPQRSTUWXYZ]{2}$/i
Upoštevajte, da to ni zelo natančno, natančnejša validacija pa je veliko bolj zapletena. Na primer, glejte StackOverflow odgovor in Wikipedia za več informacije.
Francoske poštne številke
/(^[0-8]\d\d{3}$)|(^9[0-5]\d{3}$)|(^97[1-6]\d{2}$)| (^98[46-8]\d{2}$)/
Zgornji izraz je zelo natančen in bo preveril, ali obstaja francoski departma (prvi dve števki), vključno s čezmorskimi departmaji in čezmorskimi ozemlji (DOM-TOM).
Telefonske številke
Ameriške telefonske številke
/^(?:\([2-9]\d{2}\)\ ?|[2-9]\d{2}(?:\-?|\ ?))[2-9]\d {2}[- ]?\d{4}$/
oz
/^[\(\)\.\- ]{0,}[0-9]{3}[\(\)\.\- ]{0,}[0-9]{3}[\(\ )\.\- ]{0,}[0-9]{4}[\(\)\.\- ]{0,}$/
Ta druga možnost se bo ujemala z vsemi telefonskimi številkami v Kanadi in ZDA, ki vključujejo simbole brez številk, vključno z:
. ( ) - (presledek)
To vam bo omogočilo ujemanje telefonskih številk, ki so podobne spodnjim:
- (555)555 5555
- 555.555.5555
- 555 555 5555
- (555)-555-5555
- 555-555-5555
- 555555555
Avstralske telefonske številke
Naslednji vzorci se ujemajo z vsemi različnimi avstralskimi mobilnimi in stacionarnimi telefonskimi številkami, vključno s predpono države "+61":
- (02) 9123 6535
- 03 1234-5345
- 0412 345 678
- + 61 2 3456 789
Ampak ne:
- 234 3450 234
- a234 534 3432
- 134567
- 123456789013
Oklepaji, presledki in vezaji so prezrti.
Različice »Zelo natančno:«, navedene tukaj, se ujemajo s prvimi štirimi ali petimi števkami v številki, da se zagotovi, da so veljavne avstralske številke.
»Ni zelo natančno:« se ujema samo s prvo in drugo števko, zato lahko dopušča neveljavne številke.
Vse avstralske telefonske številke (mobilne in stacionarne – območna koda je potrebna)
Zelo natančno:
/^\(?(?:\+?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})$/
Ni zelo natančno:
/^(?:\+?61|0)[2-478](?:[ -]?[0-9]){8}$/
Vse avstralske telefonske številke (samo stacionarne telefonije - območna koda je potrebna)
Zelo natančno:
/^\(?(?:\+?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}$/
Ni zelo natančno:
/^(?:\+?61|\(?0)[2378]\)?(?:[ -]?[0-9]){8}$/
Stacionarne telefonske številke Novega Južnega Walesa (območna koda ni obvezna)
Zelo natančno:
/^(?:\(?(?:\+?61|0)2\)?[ -]?)?(?:3[ -]?[38]|[46-9][ -]?[ 0-9]|5[ -]?[0-35-9])(?:[ -]?[0-9]){6}$/
Ni zelo natančno:
/^(?:\(?(?:\+?61|0)2\)?(?:[ -]?[0-9]){7}[0-9]$/
Stacionarne telefonske številke Viktorije in Tasmanije (območna koda ni obvezna)
Zelo natančno:
/^(?:\(?(?:\+?61|0)3\)?[ -]?)?(?:4[ -]?[0-57-9]|[57-9][ -]?[0-9]|6[ -]?[1-67])(?:[ -]?[0-9]){6}$/
Ni zelo natančno:
/^(?:\(?(?:\+?61|0)3\)?(?:[ -]?[0-9]){7}[0-9]$/
Stacionarne telefonske številke Queenslanda (območna koda ni obvezna)
Zelo natančno:
/^(?:\(?(?:\+?61|0)7\)?[ -]?)?(?:[2-4][ -]?[0-9]|5[ -] ?[2-7]|7[ -]?6)(?:[ -]?[0-9]){6}$/
Ni zelo natančno:
/^(?:\(?(?:\+?61|0)7\)?(?:[ -]?[0-9]){7}[0-9]$/
Stacionarne telefonske številke Južne Avstralije, Severnega ozemlja, Zahodne Avstralije (območna koda ni obvezna)
Zelo natančno:
/^(?:\(?(?:\+?61|0)8\)?[ -]?)?(?:5[1-4]|6[0-8]|[7-9] [0-9])$/
Ni zelo natančno:
/^(?:\(?(?:\+?61|0)8\)?(?:[ -]?[0-9]){7}[0-9]$/
Samo avstralske številke mobilnih telefonov
Zelo natančno:
/^(?:\+?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}$/
Ni zelo natančno:
/^(?:\(?(?:\+?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])?$/
Number validation with thousand separator
Space as separator, no minus
/^(?!0)\d{1,3}(\ \d{3})*$/
Dot as separator, minus allowed
/^-?(?!0)\d{1,3}(\.\d{3})*$/
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. You can use https://extendsclass.com/regex-tester.html to visualize regular expressions.