在findall内部的正则表达式与内部计数的正则表达式 [英] Regex inside findall vs regex inside count

查看:114
本文介绍了在findall内部的正则表达式与内部计数的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是对>如何计算字符中的字符的跟进问题字符串?

This is a follow up question to How to count characters in a string? and to Find out how many times a regex matches in a string in Python

我要计算字符串中的所有字母字符:

I want to count all alphabet characters in the string:

'Go until jurong point, crazy.. Available only in bugis n great world la e buffet... Cine there got amore wat...'

str.count()方法允许计算特定字母.使用count方法,如何计算整个字符串中的任何字母?

The str.count() method allows for counting a specific letter. How would one do that for counting any letter in the entire alphabet in a string, using the count method?

我正在尝试在count方法中使用一个正则表达式,但是它返回0而不是83.我正在使用的代码是:

I am trying to use a regex inside the count method, but it returns 0 instead of 83. The code I am using is:

import re

spam_data['text'][0].count((r'[a-zA-Z]'))

当我使用时:

len(re.findall((r'[a-zA-Z]'), spam_data['text'][0]))它返回长度为83.

为什么计数在这里返回0?

Why does count return a 0 here?

推荐答案

您应使用

You should use str.count instead of count.

spam_data['text'].str.count('\w')

0    83
Name: text, dtype: int64

要访问第一个值,请使用:

To access the first value use:

spam_data['text'].str.count('\w')[0]
83

这篇关于在findall内部的正则表达式与内部计数的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆