re.findall()函数python [重复]

rekjcdws  于 2023-01-08  发布在  Python
关注(0)|答案(1)|浏览(109)
    • 此问题在此处已有答案**:

Reference - What does this regex mean?(1个答案)
22小时前关门了。
你能帮我理解下面这行代码吗?

import re 
a= re.findall('[А-Яа-я-\s]+', string)

我对字符串中的模式有点困惑,特别是,字符串应该以A开头,以Aя之间的任何字符串结尾,应该用-和空格分隔,但是第二项Яа代表什么呢?

xzlaal3s

xzlaal3s1#

[         ]      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

[]在regex中被称为“类”,它的基本意思是“这里的任何字符都是有效的”。然后+意味着“前面的字符/类至少出现一次”。Python有一个正则表达式HowTo,你可能会发现通读它很有用。

相关问题