简体   繁体   English

如何找到所有长度为 5 且有 1 位数字和 4 个字母的字符串分成所有组组合

[英]How to find all the strings with length 5 and have 1 digit and 4 letters divided to all group combinations

I need regex to count all the groups of strings with length of 5 that contains 1 digit ( 0-9 ) and 4 small letters ( az ) with the following:我需要正则表达式来计算所有长度为 5 的字符串组,其中包含 1 位数字 ( 0-9 ) 和 4 个小写字母 ( az ),如下所示:

  • 1 digit and all letters are different 1 位数字和所有字母都不同
    For example: 1abcd例如: 1abcd
  • 1 digit, 2 letters are equal and the rest are different 1位数字,2个字母相等,其余不同
    For example: a2acd例如: a2acd
  • 1 digit, 3 letters are equal and the rest are different 1个数字,3个字母相等,其余不同
    For example: aa3ad例如: aa3ad
  • 1 digit, 4 letters are equal 1个数字,4个字母相等
    For example: aa5aa例如: aa5aa
  • 1 digit, 2 letters are equal and two different other letters are equal 1个数字,2个字母相等,另外两个不同的字母相等
    For example: 1aabb例如: 1aabb

I know how to match all the strings with length of 5 with letters and 1 digit:我知道如何将所有长度为 5 的字符串与字母和 1 位数字匹配:
^(?=.{5}$)[az]*(?:\\d[az]*){1}$
Here is an example.是一个例子。

But I don't how to do it for each of the above groups.但我不知道如何为上述每个组做这件事。
I read that for the first example ( 1 digit and all letters are different ) I need to prevent from a repeating char with .*(.).*\\1 but I tried:我读到第一个例子( 1位数字和所有字母都不同)我需要防止重复字符与.*(.).*\\1但我试过:

^(?=.{5}$)[a-z]*(?:\d[a-z]*)(.*(.).*\1){1}$  

It didn't work.它没有用。

You can use:您可以使用:

/\b(?=[a-zA-Z]*\d[a-zA-Z]*)([a-zA-Z0-9]{5})/

Demo演示

Add a second \\b to reject matching strings longer than 5 characters:添加第二个\\b以拒绝超过 5 个字符的匹配字符串:

/\b(?=[a-zA-Z]*\d[a-zA-Z]*)([a-zA-Z0-9]{5}\b)/

Demo 2演示 2

If you then want to limit to lower case letters:如果您想限制为小写字母:

/\b(?=[a-z]*\d[a-z]*)([a-z0-9]{5}\b)/

Since all combos of the four letters are possible, no further classification is necessary.由于四个字母的所有组合都是可能的,因此无需进一步分类。 All the same, all different, some the same.都一样,都不同,有些是一样的。

If you DO want to classify the letters, just capture in Python and add the logic desired.如果您确实想对字母进行分类,只需在 Python 中捕获并添加所需的逻辑即可。


Based on your example (which it would be helpful to state what is and is not a match for the goal of this question):根据您的示例(这有助于说明与此问题的目标匹配和不匹配的内容):

/(?=^[a-z]*\d[a-z]*$)(^[a-z0-9]{5}$)/mg

Demo 3演示 3

Then if you want to classify into groups, I would just do that in Python:然后,如果你想分组,我会在 Python 中这样做:

import re 

st='''\
1aaaa
2aabb
jwzw3
jlwk6
bjkgp
5fm8s
x975t
k88q5
zl796
qm9hb
h6gtf
9rm9p'''

di={}
for m in re.finditer(r'(?=^[a-z]*\d[a-z]*$)(^[a-z0-9]{5}$)', st, re.M):
    di.setdefault(len(set(m.group(1)))-1, []).append(m.group(1))

>>> di
{1: ['1aaaa'], 2: ['2aabb'], 3: ['jwzw3'], 4: ['jlwk6', 'qm9hb', 'h6gtf']}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 查找不超过给定字符长度的所有字符串组合 - Find all combinations of strings up to given character length 从字符串列表中获取所有4位数字组合 - get all 4 digit combinations out of a list of strings 如何找到所有组合 - How to find all combinations 查找 python 中 4 个数字的所有长度为 9 的组合 - Find all combinations of length 9 for of 4 numbers in python 如何不重复地生成5个字符的字符串组合(1个数字,两个相等的字母和两个不同的相等的字母) - How to generate 5-character strings combinations (1 digit, two equal letters and two different equal letters) without duplication 使用可变长度字符串的可变长度列表,如何创建所有组合 - With variable length list of variable length strings, how do I create all combinations 如何创建包含两个字符串之间所有常见字母组合的列表? - How can I create a list with all the possible combinations of common letters between two strings? 如何以组长度和组内元素的所有可能组合将列表拆分为 n 组? - How to split a list into n groups in all possible combinations of group length and elements within group? 如何在不使用迭代器和蛮力的情况下在python中找到列表中元素的所有两位数组合 - How to find all the two digit combinations of the elements in a list in python without using iterators and brute force 在九个位置的列表中查找三个字母的所有组合 - Find all combinations of three letters in a list of nine positions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM