简体   繁体   English

正则表达式帮助需要

[英]regex expression help needed

I was wondering if this was possible using Regex. 我想知道这是否可以使用正则表达式。 I would like to exclude all letters (upper and lowercase) and the following 14 characters ! 我想排除所有字母(大写和小写)以及以下14个字符! “ & ' * + , : ; “&'* +,:; < = > @ _ <=> @ _

The problem is the equal sign. 问题是等号。 In the string (which must either be 20 or 37 characters long) that I will be validating, that equal sign must either be in the 17th or 20th position because it is used as a separator in those positions. 在我要验证的字符串(必须是20或37个字符长)中,该等号必须位于第17或第20位,因为它在这些位置用作分隔符。 So it must check if that equal sign is anywhere other than in the 16th or 20th position (but not both). 因此,必须检查该等号是否在第16或第20位以外的任何位置(但不是两者)。 The following are some examples: 以下是一些例子:

pass: 1234567890123456=12345678901234567890 传球:1234567890123456 = 12345678901234567890

pass: 1234567890123456789=12345678901234567 传球:1234567890123456789 = 12345678901234567

don't pass: 123456=890123456=12345678901234567 不通过:123456 = 890123456 = 12345678901234567

don't pass: 1234567890123456=12=45678901234567890 不通过:1234567890123456 = 12 = 45678901234567890

I am having a hard time with the part that I must allow the equal sign in those two positions and not sure if that's possible with Regex. 我正在努力工作,我必须在这两个位置允许等号,并且不确定是否可以使用正则表达式。 Adding an if-statement would require substantial code change and regression testing because this function that stores this regex currently is used by many different plug-ins. 添加if语句需要大量的代码更改和回归测试,因为当前存储此正则表达式的函数被许多不同的插件使用。

I'll go for 我会去的

^([^a-zA-Z!"&'*+,:;<=>@_]{16}=[^a-zA-Z!"&'*+,:;<=>@_]+|[^a-zA-Z!"&'*+,:;<=>@_]{19}=[^a-zA-Z!"&'*+,:;<=>@_]*)$

Explanations : 解释:

1) Start with your allowed char : 1)从您允许的字符开始:

^[^a-zA-Z!"&'*+,:;<=>@_]$

[^xxx] means all except xxx, where az is lower case letters AZ upper case ones, and your others chars [^ xxx]表示除xxx以外的所有内容,其中az是小写字母AZ大写字母,以及您的其他字符

2) Repeat it 16 times, then =, then others allowed chars ("allowed char" followed by '+' to tell that is repeated 1 to n times) 2)重复16次,然后=,然后其他人允许使用字符(“允许字符”后跟“+”表示重复1到n次)

^[^a-zA-Z!"&'*+,:;<=>@_]{16}=[^a-zA-Z!"&'*+,:;<=>@_]+$

At this point you'll match your first case, when = is at position 17. 此时,您将匹配您的第一个案例,当=在第17个位置时。

3) Your second case will be 3)你的第二个案例是

^[^a-zA-Z!"&'*+,:;<=>@_]{19}=[^a-zA-Z!"&'*+,:;<=>@_]*$

with the last part followed by * instead of + to handle strings that are only 20 chars long and that ends with = 最后一部分后跟*而不是+来处理只有20个字符长并以=结尾的字符串

4) just use the (case1|case2) to handle both 4)只需使用(case1 | cas​​e2)来处理两者

^([^a-zA-Z!"&'*+,:;<=>@_]{16}=[^a-zA-Z!"&'*+,:;<=>@_]+|[^a-zA-Z!"&'*+,:;<=>@_]{19}=[^a-zA-Z!"&'*+,:;<=>@_]*)$

Tested OK with notepad++ and your examples 用notepad ++和你的例子测试好了


Edit to match exactly 20 or 37 chars 编辑以匹配20或37个字符

^([^a-zA-Z!"&'*+,:;<=>@_]{16}=[^a-zA-Z!"&'*+,:;<=>@_]{3}|[^a-zA-Z!"&'*+,:;<=>@_]{16}=[^a-zA-Z!"&'*+,:;<=>@_]{20}|[^a-zA-Z!"&'*+,:;<=>@_]{19}=|[^a-zA-Z!"&'*+,:;<=>@_]{19}=[^a-zA-Z!"&'*+,:;<=>@_]{17})$

More readable view with explanation : 更具可读性的视图和说明:

`
^(                              
                                // 20 chars with = at 17
    [^a-zA-Z!"&'*+,:;<=>@_]{16} // 16 allowed chars
    =                           // followed by =
    [^a-zA-Z!"&'*+,:;<=>@_]{3}  // folowed by 3 allowed chars
|                               
    [^a-zA-Z!"&'*+,:;<=>@_]{16} // 37 chars with = at 17   
    =                           
    [^a-zA-Z!"&'*+,:;<=>@_]{20}
|
    [^a-zA-Z!"&'*+,:;<=>@_]{19} // 20 chars with = at 20   
    =
|
    [^a-zA-Z!"&'*+,:;<=>@_]{19} // 37 chars with = at 20
    =
    [^a-zA-Z!"&'*+,:;<=>@_]{17}
)$

` `

I've omitted other symbols matching other symbols and just placed the [^=] , you should have there code for all allowed symbols except = 我省略了与其他符号匹配的其他符号,只是放置了[^=] ,你应该有除了=之外的所有允许符号的代码

var r = new Regex(@"^(([0-9\:\<\>]{16,16}=(([0-9\:\<\>]{20})|([0-9\:\<\>]{3})))|(^[^=]{19,19}=(([0-9\:\<\>]{17}))?))$");
/*
    @"^(

    ([0-9\:\<\>]{16,16}
    =
    (([0-9\:\<\>]{20})|([0-9\:\<\>]{3})))

    |

    (^[^=]{19,19}
    =
    (([0-9\:\<\>]{17}))?)

    )$"
*/

using {length,length} you can also specify the overall string length. 使用{length,length}您还可以指定整个字符串长度。 The $ in the end and ^ in the beginning are important also. 最后的$和开头的^也很重要。

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

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