Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用python正则表达式计算文本中紧跟特殊字符的单词的出现次数_Python_Regex_Counter - Fatal编程技术网

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

如何使用python正则表达式计算文本中紧跟特殊字符的单词的出现次数,python,regex,counter,Python,Regex,Counter,我想使用python计算“people”一词在文本中出现的次数。为此,我使用计数器和Python正则表达式: for j in range(len(paragraphs)): text = paragraphs[j].text count[j] = Counter(re.findall(r'\bpeople\b' ,text)) 然而,在这里我的代码没有考虑人的出现。人人 当单词后跟一个特定字符时,如何修改它以同时计算大小写 感谢您的帮助,您可以在正则表

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

    for j in range(len(paragraphs)):
        text = paragraphs[j].text
        count[j] = Counter(re.findall(r'\bpeople\b' ,text))
然而,在这里我的代码没有考虑人的出现。人人 当单词后跟一个特定字符时,如何修改它以同时计算大小写


感谢您的帮助,

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

r'\bpeople[.,!?]?\b'
那个?指定它可以出现0次或1次-
[]
指定允许的字符。无需转义
(或f.e.
()*+?
)内部的
[]
,尽管它们对正则表达式有特殊意义。如果您想在
[]
内部使用
-
,则需要将其转义,因为它用于表示集合中的范围
[1-5]
=
12345

见:

[]用于指示一组字符。一组:

可以单独列出字符,例如,[amk]将匹配“a”、“m”或“k”。 字符范围可以通过给出两个字符并用“-”分隔来表示,例如[a-z]将匹配任何小写ASCII字母,[0-5][0-9]将匹配从00到59的所有两位数字,[0-9A-Fa-f]将匹配任何十六进制数字。[……]


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

r'\bpeople[.,!?]?\b'
那个?指定它可以出现0次或1次-
[]
指定允许的字符。无需转义
(或f.e.
()*+?
)内部的
[]
,尽管它们对正则表达式有特殊意义。如果您想在
[]
内部使用
-
,则需要将其转义,因为它用于表示集合中的范围
[1-5]
=
12345

见:

[]用于指示一组字符。一组:

可以单独列出字符,例如,[amk]将匹配“a”、“m”或“k”。 字符范围可以通过给出两个字符并用“-”分隔来表示,例如[a-z]将匹配任何小写ASCII字母,[0-5][0-9]将匹配从00到59的所有两位数字,[0-9A-Fa-f]将匹配任何十六进制数字。[……]

这将允许您仅与人匹配?人和/或人

因此,如果您再添加一些
计数器(关于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正则表达式时,这也是一个很好的资源:该站点还提供了正则表达式备忘单

这将允许您仅与人匹配?人。和/或人

因此,如果您再添加一些
计数器(关于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正则表达式时,这也是一个很好的资源:该网站还有一个正则表达式备忘单

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

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

?表示零个或多个量词。上述模式似乎在regex101.com上有效,但我还没有在Python shell中试用过。

您可以在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中试用过。

它必须使用regex吗?为什么不只是:

len(text.split("people"))-1

它必须使用正则表达式吗?为什么不:

len(text.split("people"))-1

您的正则表达式看起来正确(并且适用于我的测试)。我认为您的错误是
计数器()
。您看到了什么错误?您的正则表达式看起来正确(并且适用于我的测试)。我认为您的错误是
计数器()
。您看到了什么错误?无需将
放入[]除非您也希望将其作为字符使用,否则无需在[]中放置
|
,除非您也希望将其作为字符使用。我点击了头部并编辑了我的回复以删除额外的反斜杠。我点击了头部并编辑了回复以删除额外的反斜杠。