简体   繁体   English

Python 字符串中的正则表达式

[英]Regular Expression in Python strings

I want to validate a string that satisfies the below three conditions using regular expression我想使用正则表达式验证满足以下三个条件的字符串

  1. The special characters allowed are (. , _, - ).允许的特殊字符是 (. , _, - )。
  2. Should contain only lower-case characters.应仅包含小写字符。
  3. Should not start or end with special character.不应以特殊字符开头或结尾。

To satisfy the above conditions, I have created a format as below为了满足上述条件,我创建了如下格式

^[^\W_][a-z\.,_-]+

This pattern works fine up to second character.这种模式可以正常工作到第二个字符。 However, this pattern is failing for the 3rd and subsequent characters if those contains any special character or upper cases characters.但是,如果第 3 个和后续字符包含任何特殊字符或大写字符,则此模式将失败。

Example:例子:

Pattern Works for the string S@yanthan but not for Sa@yanthan. Pattern 适用于字符串 S@yanthan,但不适用于 Sa@yanthan。 I am expecting that pattern to pass even if the third and subsequent characters contains any special characters or upper case characters.即使第三个和后续字符包含任何特殊字符或大写字符,我也希望该模式能够通过。 Can you suggest me where this pattern goes wrong please?你能建议我这种模式哪里出错了吗? Below is the snippet of the code.下面是代码片段。

import re 

a = "Sayanthan"
exp = re.search("^[^\W_][a-z\.,_-]+",a)

if exp:
    print(True)
else:
    print(False)

Based on you initial rules I'd go with:根据您的初始规则,我将 go 与:

^[a-z](?:[.,_-]*[a-z])*$

See the online demo .请参阅在线演示

However, you mentioned in the comments:但是,您在评论中提到:

"Also the third condition is "should not start with Special character" instead of "should not start or end with Special character"" "另外第三个条件是"不应该以特殊字符开头"而不是"不应该以特殊字符开头或结尾""

In that case you could use:在这种情况下,您可以使用:

^[a-z][-.,_a-z]*$

See the online demo查看在线演示

The pattern that you tried ^[^\W_][az.,_-]+ starts with [^\W_] which will match any word char except an underscore, so it could also be an uppercase char.您尝试的模式^[^\W_][az.,_-]+[^\W_]开头,它将匹配除下划线以外的任何单词字符,因此它也可以是大写字符。

Then [az.,_-]+ will match 1+ times any of the listed, which means the string can also end with a comma for example.然后[az.,_-]+将匹配任何列出的 1+ 次,这意味着字符串也可以以逗号结尾。

Looking at the conditions listed, you could use:查看列出的条件,您可以使用:

^[a-z](?:[a-z.,_-]*[a-z])?\Z
  • ^ Start of string ^字符串开头
  • [az] Match a lower case char az [az]匹配小写字符 az
  • (?: Non capture group (?:非捕获组
    • [az.,_-]*[az] Match 0+ occurrences of the listed ending with az [az.,_-]*[az]匹配 0+ 个以 az 结尾的列表
  • )? Close group and make it optional关闭组并使其成为可选
  • \Z End of string \Z字符串结束

Regex demo正则表达式演示

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

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