Actions

Using regular expressions/zh-cn: Difference between revisions

From LimeSurvey Manual

No edit summary
No edit summary
Line 259: Line 259:
<source lang="html">/^\$?\d{1,3}(\d+(?!,))?(,\d{3})*(\.\d{2})?$/</source>
<source lang="html">/^\$?\d{1,3}(\d+(?!,))?(,\d{3})*(\.\d{2})?$/</source>


==Swiss price==
==瑞士价格==


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:

Revision as of 09:33, 21 October 2020

正则表达式必须以斜杠("/")作为开头和结尾。你可以在 http://www.regexlib.net/ 找到一个很好的参考示例。如果都有斜杠包围,那么这些模板都可以工作。

你可以通过 正则表达式测试器来测试你的正则表达式。

为了完善百科的这一部分,我们诚挚的建议你将你测试过的成功的正则表达式加入进来,以便使新的 LimeSurvey (潜在的) 用户能够更好的理解这一功能。
  Attention : 请注意下面所有的例子都是在一行内完成!



重要:条件中的正则表达式

请注意在条件编辑器中使用正则表达式,不要包含前后的斜杠。


邮件地址验证

(2018/12 更新): 从这些日子开始,域名不仅可以包含特殊字符,像 “ü” ,还可以使用超过3个字符的名字作为域名,像 tech 或是 company,下面的邮件地址正则表达式只检查输入的邮件地址里是否包含“@” 和 “.” 。

/^.+?@.+?\..+$/

这个过时的正则表达式中,域名被限制在3个字符以内,不能覆盖所有的可用域名:

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

邮政编码

澳大利亚的邮政编码

/^[0-9]{4}/

巴西的邮政编码

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

加拿大的邮政编码

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

美国邮政编码

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

以零开头的邮政编码,使用:

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

英国邮政编码

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

注意这个并不是非常准确,一个精确的验证会非常复杂。比如: 堆栈溢出的答案维基百科 有更多相关的信息。

法国邮政编码

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

上面的表达式非常精确,可以用来验证法国的部门是否存在 (前两个数字), 包含海外的部门和海外属地(DOM-TOM).


电话号码

美国电话号码

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

或者

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

第二个选项将匹配所有的包括非数字标识的加拿大和美国的电话号码,包括:

 . ( ) - (space)

这个将允许你匹配类型下面的电话号码:

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

澳大利亚电话号码

下面的模式匹配了所有各种各样的澳大利亚的移动电话和座机的号码,还包括有 "+61" 国家号码前缀的:

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

但不包括:

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

括号,空格和横线都会被忽略掉。

这里的'非常精确'的意思:此处罗列的版本和电话的前4个或前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})$/

不很精确:

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

所有澳大利亚电话号码 (移动电话和座机 - 需要区号)

非常精确:

/^\(?(?:\+?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)[2378]\)?(?:[ -]?[0-9]){8}$/

新南威尔士州的固定电话(区号可选)

非常精确:

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

不很精确:

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

维多利亚和塔斯马尼亚州的固定电话(区号可选)

非常精确:

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

不很精确:

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

昆士兰固定电话号码(区号可选)

非常精确:

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

不很精确:

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

南澳大利亚,北领地,西澳大利亚的座机电话号码(区号可选)

非常精确:

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

不很精确:

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

仅限澳大利亚手机号码

非常精确:

/^(?:\+?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\)?(?:[ -]?[0-9]){7}[0-9]$/

比利时电话号码

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

比利时手机号码

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

法国电话号码

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

法国移动电话号码

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

卢森堡电话号码

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

卢森堡移动电话号码

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

德国标记(带有可选的正负号)

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


年龄验证

示例:年龄 20-99

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

示例:年龄 18-35

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

示例:年龄 19-65

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


数字验证

从 1 到 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

从 1 到 999, 从1.000 到 999.999 到 999.999.999 的数字

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

匹配从 1 到 999, 1.000 到 999.999 到 999.999.999 的数字,不匹配像 999.1.1 , 94.22.22, 999.1.22, 999.11.1, 999.1.333 的数字。

带有可选小数的数字验证(价格)

匹配从 0 到 199 的数字, 可选是否带两位小数:

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

用千位分隔符进行数字验证

空格做分隔符,没有减号

/^(?!0)\d{1,3}(\ \d{3})*$/

点做分隔符,允许减号

/^-?(?!0)\d{1,3}(\.\d{3})*$/

强制两位小数,匹配从 1.00 到 999,999,999.00 的数字,并可用逗号分隔千万/百万,以下数字都是匹配的: 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}$/

和上面相似:强制两位小数,但可以在小数点分隔符 "," 前面加 "0" 。

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

和上面一样,但两位小数变成可选项:

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

月份 (1-12)

如果你要验证某人输入的出生月份是否正确可以使用下面的表达式:

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

最小宽度 (本示例中设置为 3 )

/^.{3,}$/


货币

美元(美元符号和美分可选)

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

检查逗号用法:

/^\$?\d{1,3}(\d+(?!,))?(,\d{3})*(\.\d{2})?$/

瑞士价格

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.