如何使用python正则表达式计算文本中紧跟特殊字符的单词的出现次数

2024-04-20 16:10:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用python计算“people”这个词在文本中出现的次数。为此,我使用了Counter和Python的正则表达式:

    for j in range(len(paragraphs)):
        text = paragraphs[j].text
        count[j] = Counter(re.findall(r'\bpeople\b' ,text))

然而,这里我的代码没有考虑到人的出现。人!人? 当单词后跟一个特定的字符时,如何修改它来计算大小写?你知道吗

谢谢你的帮助


Tags: 代码textin文本reforlencount
3条回答
people[?.!]

这将允许你只与人匹配?人。和/或人!你知道吗

因此,如果您再添加一些Counter(re.finall(,您就可以这样做了

#This will only match people
count[j] = Counter(re.findall(r'people\s' ,text))

#This will only match people?
count[j] = Counter(re.findall(r'people\?' ,text))

#This will only match people.
count[j] = Counter(re.findall(r'people\.' ,text))

#This will only match people!
count[j] = Counter(re.findall(r'people\!' ,text))

您需要使用\来转义特殊字符

此外,当您在试用python正则表达式时,这也是一个很好的资源:https://pythex.org/该站点还有一个正则表达式备忘单

您可以在Regex模式的“people”部分的末尾使用修饰符语句。请尝试以下操作:

for j in range(len(paragraphs)):
    text = paragraphs[j].text
    count[j] = Counter(re.findall('r\bpeople[.?!]?\b', text)

那个?表示零个或多个量词。上面的模式似乎在regex101.com上可以使用,但我还没有在Python shell中试用过。你知道吗

您可以在正则表达式中使用可选字符组:

r'\bpeople[.,!?]?\b'

那个?指定它可以出现0或1次[]指定允许的字符。无需转义.(或f.e.()*+?)内部的[],尽管它们对regex有特殊的意义。如果要在[]中使用-,则需要对其进行转义,因为它用于表示集合中的范围[1-5]==12345。你知道吗

见:https://docs.python.org/3/library/re.html#regular-expression-syntax

[] Used to indicate a set of characters. In a set:

Characters can be listed individually, e.g. [amk] will match 'a', 'm', or 'k'. Ranges of characters can be indicated by giving two characters and separating them by a '-', for example [a-z] will match any lowercase ASCII letter, [0-5][0-9] will match all the two-digits numbers from 00 to 59, and [0-9A-Fa-f] will match any hexadecimal digit. [...]

相关问题 更多 >