简体   繁体   English

有人可以解释一下 python 中的这个正则表达式模式吗? re.findall("[a-zA-Z*,*\-.*.]"

[英]Can some one explain me the this regex pattern in python? re.findall("[a-zA-Z*,*\-!*.]"

re.findall("[a-zA-Z*,*\-!*.]"

This regular expression checks for valid letters, ".", "-", "-", "!"此正则表达式检查有效字母“.”、“-”、“-”、“!” in a word.一句话。 I understood the first part我明白了第一部分

[a-zA-Z]

Can someone please explain this?有人可以解释一下吗?

[*,*\-!*.]

You can use the online tool regex101 to understand your regex and test it, for example, here is the explanation for this regex:您可以使用在线工具regex101来了解您的正则表达式并对其进行测试,例如,这里是这个正则表达式的解释:

Match a single character present in the list below [a-zA-Z*,*\-!*.]
a-z matches a single character in the range between a (index 97) and z (index 122) (case sensitive)
A-Z matches a single character in the range between A (index 65) and Z (index 90) (case sensitive)
*,* matches a single character in the list *, (case sensitive)
* matches the character * with index 4210 (2A16 or 528) literally (case sensitive)
, matches the character , with index 4410 (2C16 or 548) literally (case sensitive)
\- matches the character - with index 4510 (2D16 or 558) literally (case sensitive)
!*. matches a single character in the list !*. (case sensitive)
! matches the character ! with index 3310 (2116 or 418) literally (case sensitive)
* matches the character * with index 4210 (2A16 or 528) literally (case sensitive)
. matches the character . with index 4610 (2E16 or 568) literally (case sensitive)

That's a badly designed regex.这是一个设计糟糕的正则表达式。

[*,*\-.*.] it's looking for one of the characters * , , , - , ! [*,*\-.*.]它正在寻找字符之一* , , , - , ! or .. . .

The * appears three times, which is unnecessary since the regex is going to be consumed after a single character appears. *出现 3 次,这是不必要的,因为正则表达式将在单个字符出现后被消耗。

Whoever wrote it was probably thinking in doing any letter, , , - , !写它的人可能正在考虑写任何信, - ! or .. 0 or more times , so the correct regex would be [a-zA-Z,\-..]* . 0 次或多次,所以正确的正则表达式是[a-zA-Z,\-..]*

Example:例子:

example = 'foo,bar,baz!'
print(re.findall("[a-zA-Z*,*\-!*.]", example))
print(re.findall("[a-zA-Z,\-!*.]", example))  # same as before, but with only one *
print(re.findall("[a-zA-Z,\-!.]*", example))

outputs:输出:

['f', 'o', 'o', ',', 'b', 'a', 'r', ',', 'b', 'a', 'z', '!']
['f', 'o', 'o', ',', 'b', 'a', 'r', ',', 'b', 'a', 'z', '!']
['foo,bar,baz!', '']

(the empty string is due to the * , which allows for no-occurrences to be recognized positively, and could be fixed by using + instead) (空字符串是由于* ,它允许肯定地识别不出现,并且可以通过使用+来修复)

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

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