python中re的常见用法总结(update)

本文深入探讨了正则表达式中look_ahead和look_behind的使用方法,详细解释了如何利用这些特性进行复杂匹配和替换操作,特别强调了固定宽度匹配的重要性及如何在替换过程中保留并使用搜索模式的部分内容。

1. look_ahead or look_after:

i) 当且仅当某类字符串之前符合某条件时,才把该字符串替换为另一类字符串。

    如:当“$51%r" 前为 |, -, % 或者-*时,把其替换为a51%r.

           re.sub((?<=.\||.-|-\*|.\$)\$51%r, a51%r)

   注意: look_ahead里的模式要求,模式匹配的字符串必须是固定宽度的。比如上例都是2个字符。必须用”.-"和“.\|",而不能仅仅用”-“和"\|",因为”-\*"占了两个字符,所以其他的也都必须两个字符。

2. 保留搜索模式的某部分不改变,记录下来,留给替换用字符串使用。

            ii) 比如上例中的”51“,实际上可能会是”35“, ”55“等,我要把他们记录下来,并且用在替换字符串里。即,匹配字符串里是”$51"那么替换字符串就是“a51",如果匹配字符串里是”$35",那么替换字符串里就是“a35”。

                                  

         re.sub((?<=.\||.-|-\*|.\$)\$(?P<name>51|35|21)%r, a\g<name>%r)

            (?P<name>...)用来匹配模式,并且将该模式匹配的内容记录下来,用变量“name”来表示(这个name可以更改),然后在使用\g<name>来表示这段匹配的内容。

<think>根据用户的问题,他想要获取常见英文单词列表,以便用于驼峰命名的转换。在之前的回答中,我们使用了一个预定义的常见单词集合(如'client', 'code'等)。但是,用户希望有一个更通用的方法来获取常用英文单词列表。 我们可以从以下几个方面考虑: 1. 使用Python的内置资源:Python标准库中并没有直接提供常用单词列表,但有一些资源可以利用。 2. 使用外部库:例如`nltk`(自然语言工具包)提供了英语单词语料库。 3. 使用开源单词列表:可以从网络上下载常用英语单词列表,然后读取到Python中。 这里,我推荐使用`nltk`库,因为它包含多种语料库,其中就有常用英语单词。 步骤: 1. 安装nltk库(如果尚未安装):`pip install nltk` 2. 下载nltk的单词语料库(例如:words语料库) 3. 将单词转换为小写并去重,得到一个常用单词集合。 注意:常用单词语料库可能包含大量单词(通常超过20万),但我们可能只需要最常用的单词(比如前5000或10000个)。我们可以根据频率来筛选,但nltk的words语料库不包含频率信息。因此,我们可以使用另一个语料库:`brown`(布朗语料库)来获取单词频率。 另一种方法是使用预制的常用单词列表,例如“2000个最常用英语单词”等。 这里提供两种方法: 方法一:使用nltk的words语料库(全部单词,约23万) 方法二:使用nltk的布朗语料库统计频率,然后取前N个。 考虑到驼峰命名转换中,我们通常需要的是常见的、较短的单词(如名词、动词等),我们可以选择使用一个现成的常用单词列表,或者使用布朗语料库的前5000个高频词。 由于用户可能不需要全部单词,我们可以选择获取前10000个高频词。 实现步骤: 方法一:直接使用nltk的words语料库(不区分频率,但包含常见单词): ```python import nltk nltk.download('words') from nltk.corpus import words common_words = set(words.words()) # 全部单词,大小写混合 ``` 但是,这个列表包含了很多生僻词,而且有大小写。我们可以全部转为小写,然后过滤掉长度过短的单词(比如长度至少为2)?不过,驼峰拆分中,我们可能需要短单词(如'id','by'等),所以不过滤长度。 方法二:使用布朗语料库统计高频词(更接近常用单词): ```python import nltk nltk.download('brown') from nltk.corpus import brown from collections import Counter # 统计布朗语料库中所有单词的频率 freq_dist = Counter(brown.words()) # 取前10000个高频词 common_words = set([word.lower() for word, count in freq_dist.most_common(10000)]) ``` 方法二可能更符合“常用”的概念。 另外,我们还可以结合多个来源,比如使用nltk的stopwords(停用词,这些词非常常见)作为基础,再加上一些技术常用词(如'id','http'等)。 但是,考虑到驼峰命名转换中,我们可能还需要一些在编程中常见的缩写或术语(如'http','id','url'等),我们可以将停用词和自定义的技术词汇合并。 这里,我建议使用停用词加上一个自定义的技术词汇列表,再结合布朗语料库的前N个高频词。 然而,为了简单起见,我们可以先使用停用词和技术词汇的合并。 步骤: 1. 下载nltk停用词语料库 2. 获取停用词(这些词都是常用词) 3. 添加自定义的技术词汇 示例代码: ```python import nltk nltk.download('stopwords') from nltk.corpus import stopwords # 获取英文停用词(小写) stop_words = set(stopwords.words('english')) # 添加自定义技术词汇 tech_words = {'id', 'http', 'url', 'html', 'xml', 'json', 'api', 'user', 'client', 'code', 'project', 'status', 'equip', 'stage', 'machine', 'update', 'change', 'no', 'date', 'is', 'summary'} common_words = stop_words | tech_words # 取并集 ``` 这样得到的common_words大小约为200个左右(停用词约179个,加上自定义词汇),可以满足基本的驼峰拆分需求。 但是,如果用户需要更全面的常用单词,建议使用方法二(布朗语料库前10000个高频词)并加入自定义技术词汇。 综合方案:我们结合停用词、自定义技术词汇和布朗语料库的前10000个高频词(取并集),这样既保证了常用性,又包含技术词汇。 然而,考虑到布朗语料库的前10000个高频词已经包含了几乎所有的停用词,所以我们可以只使用布朗语料库的高频词加上自定义技术词汇。 实现: ```python import nltk from nltk.corpus import brown from collections import Counter # 下载布朗语料库(如果未下载) nltk.download('brown') # 统计词频 freq_dist = Counter(word.lower() for word in brown.words()) # 取前10000个高频词 common_words = set(word for word, count in freq_dist.most_common(10000)) # 添加自定义技术词汇 tech_words = {'id', 'http', 'url', 'html', 'xml', 'json', 'api', 'uuid', 'guid'} common_words.update(tech_words) ``` 注意:这样得到的common_words是一个小写单词的集合。 在驼峰拆分函数中,我们可以这样使用: 在拆分连续小写字符串时,我们使用这个common_words集合来匹配单词。 但是,由于我们获取的单词列表比较大(10000个),可能会影响拆分效率。不过,拆分时我们是从长到短匹配,一旦匹配到就断开,所以实际效率还是可以接受的。 另外,我们可以将common_words保存为文件,避免每次运行都重新计算。 考虑到用户可能没有安装nltk,或者不想下载语料库,我们可以提供一个默认的常用单词列表(比如使用停用词+自定义词汇)作为备选。 现在,根据用户的需求,他只需要获取常用英文单词列表的方法。我们可以提供多种方法供选择。 总结获取常用英文单词列表的方法: 方法1:使用nltk停用词(约179个)并添加自定义词汇(推荐轻量级使用) 方法2:使用布朗语料库高频词(前10000个)并添加自定义词汇(推荐更全面的单词) 方法3:使用nltk的words语料库(全部单词,23万多个,不推荐,因为包含生僻词) 方法4:从网络下载常用单词列表,例如使用请求获取一个在线的常用单词列表。 这里提供方法1和方法2的代码示例。 另外,如果用户不想使用nltk,我们可以提供一个简单的常用单词列表(例如:从内置的停用词列表开始,然后添加一些自定义词)。但是,停用词列表是nltk的一部分,所以没有nltk的话,我们可以自己写一个停用词列表(网上有常见的停用词列表)。 这里,我们给出两种方法的代码: 方法1:使用停用词和自定义词汇(需要nltk) 方法2:不使用nltk,使用一个预定义的停用词列表(从网上找一个常见的停用词列表,然后加上自定义词汇) 由于停用词列表较小,我们可以直接复制一个常见的停用词列表。 下面,我们提供两种实现: 实现一(需要安装nltk): ```python def get_common_words_with_nltk(): try: from nltk.corpus import stopwords from nltk import download download('stopwords', quiet=True) stop_words = set(stopwords.words('english')) except: # 如果失败,使用一个默认的停用词列表(小部分) stop_words = {'i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', "you're", "you've", "you'll", "you'd", 'your', 'yours', 'yourself', 'yourselves', 'he', 'him', 'his', 'himself', 'she', "she's", 'her', 'hers', 'herself', 'it', "it's", 'its', 'itself', 'they', 'them', 'their', 'theirs', 'themselves', 'what', 'which', 'who', 'whom', 'this', 'that', "that'll", 'these', 'those', 'am', 'is', 'are', 'was', 'were', 'be', 'been', 'being', 'have', 'has', 'had', 'having', 'do', 'does', 'did', 'doing', 'a', 'an', 'the', 'and', 'but', 'if', 'or', 'because', 'as', 'until', 'while', 'of', 'at', 'by', 'for', 'with', 'about', 'against', 'between', 'into', 'through', 'during', 'before', 'after', 'above', 'below', 'to', 'from', 'up', 'down', 'in', 'out', 'on', 'off', 'over', 'under', 'again', 'further', 'then', 'once', 'here', 'there', 'when', 'where', 'why', 'how', 'all', 'any', 'both', 'each', 'few', 'more', 'most', 'other', 'some', 'such', 'no', 'nor', 'not', 'only', 'own', 'same', 'so', 'than', 'too', 'very', 's', 't', 'can', 'will', 'just', 'don', "don't", 'should', "should've", 'now', 'd', 'll', 'm', 'o', 're', 've', 'y', 'ain', 'aren', "aren't", 'couldn', "couldn't", 'didn', "didn't", 'doesn', "doesn't", 'hadn', "hadn't", 'hasn', "hasn't", 'haven', "haven't", 'isn', "isn't", 'ma', 'mightn', "mightn't", 'mustn', "mustn't", 'needn', "needn't", 'shan', "shan't", 'shouldn', "shouldn't", 'wasn', "wasn't", 'weren', "weren't", 'won', "won't", 'wouldn', "wouldn't"} # 添加自定义技术词汇 tech_words = {'id', 'http', 'url', 'html', 'xml', 'json', 'api', 'uuid', 'guid', 'client', 'code', 'project', 'no', 'status', 'equip', 'stage', 'machine', 'update', 'change', 'date', 'is', 'summary', 'user', 'find', 'by'} return stop_words | tech_words ``` 实现二(不需要nltk,使用布朗语料库高频词,需要下载语料库): ```python def get_common_words_from_brown(top_n=10000): try: from nltk.corpus import brown from nltk import download from collections import Counter download('brown', quiet=True) freq_dist = Counter(word.lower() for word in brown.words()) common_words = set(word for word, count in freq_dist.most_common(top_n)) # 添加自定义技术词汇 tech_words = {'id', 'http', 'url', 'html', 'xml', 'json', 'api', 'uuid', 'guid', 'client', 'code', 'project', 'no', 'status', 'equip', 'stage', 'machine', 'update', 'change', 'date', 'is', 'summary', 'user', 'find', 'by'} common_words.update(tech_words) return common_words except: # 如果失败,回退到get_common_words_with_nltk return get_common_words_with_nltk() ``` 注意:两个函数都添加了相同的自定义技术词汇。 用户可以根据需要选择使用哪个函数。如果用户需要更全面的单词列表,建议使用`get_common_words_from_brown`(但第一次运行需要下载语料库,可能较慢)。如果用户只需要基本单词,使用`get_common_words_with_nltk`即可。 在驼峰转换函数中,我们可以这样调用: ```python COMMON_WORDS = get_common_words_with_nltk() # 或者使用get_common_words_from_brown() ``` 最后,如果用户既不想安装nltk,也不想使用网络下载,我们可以提供一个完全独立的函数,使用内置的停用词列表和自定义词汇(如实现一中的except部分那样)。 因此,我们可以这样修改驼峰转换函数中的`COMMON_WORDS`定义: 在函数内部,我们使用: ```python # 常见英文单词列表(使用独立函数获取) COMMON_WORDS = get_common_words_with_nltk() # 独立函数,内部处理了异常 ``` 但是,为了代码的简洁,我们也可以将停用词列表直接内置在函数中(避免依赖nltk)。 考虑到用户可能没有安装nltk,我们提供一个独立于nltk的版本: 修改驼峰转换函数,使用内置的停用词列表(上面实现一中的except部分)加上自定义词汇。 这样,整个驼峰转换函数就不需要依赖nltk了。 所以,我们修改之前的驼峰转换函数,将`COMMON_WORDS`定义为一个内置的集合(包含停用词和自定义词汇)。 但是,由于停用词列表较长,为了代码可读性,我们可以将停用词列表放在函数外部(作为模块级常量)。 由于用户要求写一个函数,我们可以将停用词列表放在函数内部。 现在,我们重构之前的驼峰转换函数,使其不依赖nltk: ```python def to_lower_camel_case(str_list): # 内置常用单词列表(停用词+自定义技术词汇) COMMON_WORDS = { 'i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', "you're", "you've", "you'll", "you'd", 'your', 'yours', 'yourself', 'yourselves', 'he', 'him', 'his', 'himself', 'she', "she's", 'her', 'hers', 'herself', 'it', "it's", 'its', 'itself', 'they', 'them', 'their', 'theirs', 'themselves', 'what', 'which', 'who', 'whom', 'this', 'that', "that'll", 'these', 'those', 'am', 'is', 'are', 'was', 'were', 'be', 'been', 'being', 'have', 'has', 'had', 'having', 'do', 'does', 'did', 'doing', 'a', 'an', 'the', 'and', 'but', 'if', 'or', 'because', 'as', 'until', 'while', 'of', 'at', 'by', 'for', 'with', 'about', 'against', 'between', 'into', 'through', 'during', 'before', 'after', 'above', 'below', 'to', 'from', 'up', 'down', 'in', 'out', 'on', 'off', 'over', 'under', 'again', 'further', 'then', 'once', 'here', 'there', 'when', 'where', 'why', 'how', 'all', 'any', 'both', 'each', 'few', 'more', 'most', 'other', 'some', 'such', 'no', 'nor', 'not', 'only', 'own', 'same', 'so', 'than', 'too', 'very', 's', 't', 'can', 'will', 'just', 'don', "don't", 'should', "should've", 'now', 'd', 'll', 'm', 'o', 're', 've', 'y', 'ain', 'aren', "aren't", 'couldn', "couldn't", 'didn', "didn't", 'doesn', "doesn't", 'hadn', "hadn't", 'hasn', "hasn't", 'haven', "haven't", 'isn', "isn't", 'ma', 'mightn', "mightn't", 'mustn', "mustn't", 'needn', "needn't", 'shan', "shan't", 'shouldn', "shouldn't", 'wasn', "wasn't", 'weren', "weren't", 'won', "won't", 'wouldn', "wouldn't", # 自定义技术词汇 'id', 'http', 'url', 'html', 'xml', 'json', 'api', 'uuid', 'guid', 'client', 'code', 'project', 'no', 'status', 'equip', 'stage', 'machine', 'update', 'change', 'date', 'is', 'summary', 'user', 'find', 'by' } # 拆分单词的函数(同之前) def split_words(s): # ...(和之前一样)... # 转换逻辑(同之前) # ... ``` 这样,我们就完全去除了对nltk的依赖,同时保证了常用单词的覆盖。 因此,用户可以直接使用这个函数,无需安装额外的库。 注意:内置的停用词列表可能不够完整,但已经覆盖了大部分常用单词,再加上自定义技术词汇,可以满足驼峰拆分的需求。 最后,回答用户的问题:Python获取常用英文单词列表的方法有多种,我们推荐使用内置的停用词列表+自定义词汇,这样无需外部依赖。如果需要更全面的单词列表,可以使用nltk的布朗语料库高频词。 但是,在驼峰转换函数中,我们已经内置了一个常用单词列表,所以用户不需要额外获取。 所以,我们提供改进后的驼峰转换函数(不依赖nltk)如下:</think>### Python获取常用英文单词列表的方法 获取常用英文单词列表有多种方法,以下是几种实用方案: #### 方法1:使用NLTK库(推荐) ```python import nltk def get_common_words(): # 下载所需数据集 nltk.download('brown') # 布朗语料库 nltk.download('words') # 单词列表 # 从布朗语料库获取前5000高频词 from nltk.corpus import brown from collections import Counter freq_dist = Counter(word.lower() for word in brown.words()) common_words = set(word for word, _ in freq_dist.most_common(5000)) # 添加编程常用词汇 tech_words = { 'id', 'input', 'output', 'format', 'http', 'api', 'json', 'xml', 'url', 'user', 'client', 'code', 'project', 'status', 'equip', 'stage', 'machine', 'update', 'date', 'summary' } return common_words | tech_words # 使用示例 common_words = get_common_words() print(f"获取{len(common_words)}个常用单词") ``` #### 方法2:使用预定义单词列表(免安装) ```python def get_common_words(): """返回预定义的常用单词集合(编程相关)""" return { # 基础词汇 'the', 'be', 'to', 'of', 'and', 'a', 'in', 'that', 'have', 'i', 'it', 'for', 'not', 'on', 'with', 'he', 'as', 'you', 'do', 'at', # 编程常用词 'id', 'input', 'output', 'format', 'http', 'api', 'json', 'xml', 'url', 'user', 'client', 'code', 'project', 'status', 'equip', 'stage', 'machine', 'update', 'date', 'summary', 'change', 'no', 'is', 'by', 'find', 'request', 'response', 'data', 'file', 'type' } # 使用示例 common_words = get_common_words() ``` #### 方法3:从文本文件加载(自定义词库) 1. 创建`words.txt`文件: ``` id input output format http api json xml url user client ... ``` 2. Python读取代码: ```python def load_words_from_file(filename="words.txt"): with open(filename, 'r') as f: return set(line.strip().lower() for line in f) # 使用示例 common_words = load_words_from_file() ``` ### 在驼峰命名函数中的应用 ```python def to_lower_camel_case(str_list): COMMON_WORDS = get_common_words() # 使用上述任一方法获取单词列表 def split_words(s): # 实现单词拆分逻辑(使用COMMON_WORDS) ... # 剩余转换逻辑不变 ... ``` ### 不同方法的比较 | 方法 | 单词数量 | 是否需要安装 | 是否可定制 | 特点 | |------------|----------|--------------|------------|--------------------------| | NLTK库 | ~5000 | 需要 | 是 | 最全面,包含真实语料数据 | | 预定义列表 | ~100 | 不需要 | 是 | 开箱即用,轻量级 | | 文件加载 | 自定义 | 不需要 | 是 | 完全自定义,便于维护 | ### 推荐方案 1. **优先使用NLTK**:适合需要全面单词覆盖的场景 2. **预定义列表**:适合简单应用或受限环境 3. **自定义文件**:适合需要领域特定词汇的场景(如金融、医疗等) > 对于驼峰命名转换,建议使用**NLTK方法**获取单词列表,因其包含更全面的常用词汇,能更准确拆分类似`clientcode`这样的复合词[^1][^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值