简体   繁体   English

tcl regexp名称验证

[英]tcl regexp name validation

I need to validate a name in tcl, it can be: 我需要验证在TCL的名称,也可以是:

Letters (upper and lower case) 字母(大写和小写)

Numbers 号码

Any of these symbols : "_" underscore "-" dash "/" slash 这些符号中的任何一个:“ _”下划线“-”破折号“ /”斜杠

我会用:

regexp {^[\w/-]+$} $string

While the other answers can solve the problem, here an answer how you could write it yourself: 虽然其他答案可以解决问题,但是这里提供了一个答案,您可以自己编写:

  • The string should only contain the defined characters: 该字符串应仅包含定义的字符:
    The entire string have to be matched, so add constraints: ^ at the beginning (marks the beginning of the string) and $ at the end. 整个字符串必须匹配,因此添加约束: ^开头(标记字符串的开头)和$结尾。

  • The string should contain of one or more of some characters: 该字符串应包含一个或多个某些字符:
    [az]+ means one or more characters in the range (inclusive) from a to z. [az]+表示从a到z的一个或多个字符(包括)。

  • Add some more possible characters: ^[a-zA-Z_0-9/-]+$ 添加更多可能的字符: ^[a-zA-Z_0-9/-]+$
    (Note that - is at the end, otherwise it defines a range) (请注意-位于末尾,否则定义一个范围)

And voila. 和瞧。 Your regexp. 您的正则表达式。

/^[\w-\/]+$/

\\w is for letters, numbers and underscore. \\w用于字母,数字和下划线。 Source 资源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM