[英]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
),如下所示:
1abcd
例如: 1abcd
a2acd
例如: a2acd
aa3ad
例如: aa3ad
aa5aa
例如: aa5aa
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})/
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)/
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
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.