你能帮我理解下面这行代码吗?
import re
a= re.findall('[А-Яа-я-s]+', string)
我对必须在字符串中找到的模式有点困惑。特别是,字符串应该以A
开始,以A
和я
之间的任何字符串结束,应该用-
和空格分隔,但是第二项Яа
代表什么?
[ ] any of the characters in here
А-Я any character from А and Я, inclusive
а-я any character between а and я, inclusive
- the character - (this is ambiguous; it should only be at the very start or end of the class)
s any whitespace character
+ at least one of the preceding class of characters
[А-Яа-я-s]+ at least one character between А and Я (uppercase or lowercase), a dash, or whitespace
[]
被称为"类"在正则表达式中,它的意思是说,这里的任何字符都是有效的。然后+
表示"至少出现一次前面的字符/类"。Python有一个正则表达式规则,你可能会觉得阅读它很有用。