Python 仅重新匹配单词中的字母 [英] Python re match only letters from word

查看:40
本文介绍了Python 仅重新匹配单词中的字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Python re 的新手,但我需要帮助.我在这里搜索,谷歌,文档,但没有任何效果.所以这就是我想要做的.

I am new to Python re, but I need help. I searched here, google, documentation, but nothing worked. So here is what I am trying to do.

我有词(例如)字符串"然后我有单词列表:

I have word (for example) "string" then I have word list:

字符串、字符串、str、ing、in、ins、rs、压力

strings, string, str, ing, in, ins, rs, stress

我想匹配:string、str、ing、in、ins、rs.

我不想匹配:stress,strings(因为有2x s,而在word string中,只有1个)

I don't want to match: stress, strings (because there are 2x s, and in word string, there is only 1)

  • 只匹配单词string中的字母.

抱歉英语不好,如果我解释得不够好.

Sorry for bad english and if I didnt explained good enough.

是的,而且有些字母是 unicode.

YES, and also, some letters are unicode.

推荐答案

本着问题的精神,这里有一个正则表达式答案.

In the spirit of the question, here's a regex answer.

这里是正则表达式.

它是 ^(?=[string]{1,6}$)(?!.*(.).*\1).*$

这会检查 string 中字符的 1-6 次出现.后半部分确保没有重复.当然,如果您的原始 sstring 中有多个相同的字符,这种方法就会失效,而且对于长字符串来说它不是特别有效.

This checks for 1-6 occurrences of the characters within string. The second half ensures that there is no duplication. Of course, this approach breaks down if you had multiple identical characters in your original sstring, and it isn't particularly efficient for long strings.

为通用输入词运行它的代码:

The code to run it for generic input words:

import re
mylist = ["strings", "string", "str", "ing", "in", "ins", "rs", "stress"]
word = "string"
r = re.compile("^(?=[%s]{1,%d}$)(?!.*(.).*\1).*$" % (word, len(word)))
print filter(r.match, mylist)

打印:

['string', 'str', 'ing', 'in', 'ins', 'rs']

['string', 'str', 'ing', 'in', 'ins', 'rs']

您可以在此处使用代码.

这篇关于Python 仅重新匹配单词中的字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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