简体   繁体   English

字母和数字的组合匹配正则表达式

[英]Letters and digits combination match Regular Expressions

I have a challenge with regular expression match that might be just too easy for one of you. 正则表达式匹配对我来说是一个挑战,对你们中的一个人来说可能太容易了。 I hope you can help me with this problem. 希望您能帮我解决这个问题。 I need to test a string format in one shot using javascript or VB.net. 我需要使用javascript或VB.net在一次测试中测试字符串格式。

The string could have two parts separated by one tilde char ~ such as str1~str2 or just one part without the tilde character such as str1 or str2. 该字符串可以有两部分,用一个波浪号字符〜分隔,例如str1〜str2,也可以只有一个没有波浪号字符的部分,例如str1或str2。

Str1 is a sequence of 0 to 8 digits. Str1是0到8位数字的序列。 When str1 has length zero, the tilde character does not exist either. 当str1的长度为零时,波浪号字符也不存在。

Str2 has a length of 0 to 25 characters and its format must be a combination of letters and digits. Str2的长度为0到25个字符,其格式必须为字母和数字的组合。 Please note that just letters or just digits are not good but any combination of both. 请注意,仅字母或数字不好,但是两者都不能组合。 When str2 has length zero, the tilde character does not exists. 当str2的长度为零时,波浪号字符不存在。

Since the combination of str1~str2 is an optional field, both str1 and str2 may have length zero which results in empty value for str1~str2. 由于str1〜str2的组合是可选字段,因此str1和str2的长度都可能为零,这会导致str1〜str2的值为空。

For example 12345678~ab2345cdef, and 12345 and ab2345 have all correct format but 12345678~abcdefgh and 12345678~70934527654 do not have correct format because the second part is not a combination of letters and digits. 例如12345678〜ab2345cdef,12345和ab2345都具有正确的格式,但是12345678〜abcdefgh和12345678〜70934527654的格式不正确,因为第二部分不是字母和数字的组合。

I tried solutions with patters such as /^([\\d]{4,8})(~)([az\\d]{0,25})$/.test('12345678~ab2345cdef') which does not produce the right result although I get correct result for examples such as 123456 or ab1234. 我尝试使用/^([\\d]{4,8})(~)([az\\d]{0,25})$/.test('12345678~ab2345cdef')这样的模式解决方案正确的结果,尽管对于诸如123456或ab1234之类的示例,我可以获得正确的结果。

Any idea please? 有什么想法吗?

Thank you in advance 先感谢您

There are official documents, for example Regular Expressions and tutorials on Mastering Lookahead and Lookbehind . 有正式文档,例如正则表达式和有关精通Lookahead和Lookbehind的教程。
As to your example, try following: 对于您的示例,请尝试以下操作:

var re = /^(\d{4,8})*~?((?=\d*[a-z])(?=[a-z]*\d)[a-z\d]{0,25})*$/;  

//testing
re.test('12a345~jh44kjh'); //false. non-digit in 1st part
re.test('12345~jhkjh'); //false. no digits in 2nd part
re.test('12345~123:456'); //false. ":" is not allowed in 2nd part
re.test('12345~123456'); //false. no letters in 2nd part
re.test('12345~abc123456'); //true. 
re.test('12345~123456abc'); //true. 
re.test('12345~123abc456'); //true. 
re.test('12345~abc123def'); //true. 
re.test('12345'); //true. 2nd part is optional  
re.test('abc12345'); //true. 1st part is optional

Some explanations: 一些解释:
^ start of string ^字符串的开头
$ end of string $字符串结尾
(something) capturing pattern (something)捕获模式
(?=something) non-capturing lookahead pattern (?=something)非捕获超前模式
\\d{4,8} 4 to 8 digits \\d{4,8} 4至8位数字
(?=\\d*[az]) zero or more digits and a letter ahead (?=\\d*[az])零个或多个数字, 并且前面有一个字母
(?=[az]*\\d) zero or more letters and a digit ahead (?=[az]*\\d)零个或多个字母一个数字
[az\\d]{0,25} allowed symbol set: lowercase letters and digits [az\\d]{0,25}允许的符号集:小写字母和数字

Here's a solution in VB.NET. 这是VB.NET中的解决方案。 The pattern is in 3 parts rolled into one: 模式分为三部分:

Pattern for the first part ie 1-8 digits 第一部分的模式,即1-8位数字

^[0-9]{1,8}$

Pattern for the second part ie 1-25 alphanumeric with at least 1 letter or number: 第二部分的模式,即1-25个字母数字和至少1个字母或数字:

^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{1,25}$

Combination of the two, with the tilde: 两者与波浪号的组合:

^[0-9]{1,8}~(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{1,25}$

The three patterns are joined together with | 这三个模式与| to form the single regular expression. 形成单个正则表达式。

I've included a few test cases in the code. 我在代码中包含了一些测试用例。

Imports System.Text.RegularExpressions

Module Module1

    Sub Main()

        Dim Pattern As String
        Dim Parser As Regex
        Dim Tests As New List(Of String)

        Pattern = "^[0-9]{1,8}$|^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{1,25}$|^[0-9]{1,8}~(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{1,25}$"
        Parser = New Regex(Pattern)

        Tests.Add("12345678~ab2345cdef")
        Tests.Add("12345")
        Tests.Add("ab2345")
        Tests.Add("12345678~abcdefgh")
        Tests.Add("12345678~70934527654")
        Tests.Add("12a345~jh44kjh")
        Tests.Add("123456789")
        Tests.Add("ABab73895zzz")
        Tests.Add("aaaaaBBBBBCCCCCdddddZZZZZ1")
        Tests.Add("aa0aaBB1BBCC2CCdd3ddZZ4ZZ")
        Tests.Add("0~aa0aaBB1BBCC2CCdd3ddZZ4ZZ")
        Tests.Add("12345~123___aaa")
        Tests.Add("a123~z9")
        Tests.Add("1~2a")
        Tests.Add("")

        For Each Input As String In Tests
            Console.WriteLine(String.Format("{0} {1}", Input, Parser.IsMatch(Input)))
        Next

        Console.ReadKey()

    End Sub

End Module

this is my solution: 这是我的解决方案:

/^(\d{1,8}|(?=\d*[a-z])(?=[a-z]*\d)[a-z\d]{2,25}|\d{1,8}~(?=\d*[a-z])(?=[a-z]*\d)[a-z\d]{2,25})$/

\\d{1,8} from 1 to 8 numbers \\d{1,8}从1到8个数字

(?=\\d*[az])(?=[az]*\\d)[az\\d]{2,25} from 2 to 25 letters (?=\\d*[az])(?=[az]*\\d)[az\\d]{2,25}从2到25个字母

\\d{1,8}~(?=\\d*[az])(?=[az]*\\d)[az\\d]{2,25} join of above cases by ~ \\d{1,8}~(?=\\d*[az])(?=[az]*\\d)[az\\d]{2,25}通过〜连接上述情况

test: https://jsfiddle.net/s2g0b6v9/2/ 测试: https//jsfiddle.net/s2g0b6v9/2/

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM