简体   繁体   English

如何在C#正则表达式中执行AND运算符

[英]How to perform AND operator in a C# regex

I have two regular expressions that are being used for different purposes. 我有两个用于不同目的的正则表达式。

^(?:(?!(?:abc|bcd|cde|def|efg|fgh|ghi|hij|ijk|jkl|klm|lmn|mno|nop|opq|pqr|qrs|rst|stu|tuv|uvw|vwx|wxy|xyz|012|123|234|345|456|567|678|789|(0|1|2|3|4|5|6|7|8|9|a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z)'\'1{2})).)

This regex is for controlling that a string can't contain 3 consecutive characters like abc , 123 此正则表达式用于控制字符串不能包含3个连续字符,例如abc123

^['\'x20-\x7E]+$

And this regex is for controlling that a string can't contain non-english characters like ş , ü , ı , ğ ,... 这个正则表达式用于控制字符串不能包含非英语字符,例如şüığ ,...

I would like to combine these two rules. 我想将这两个规则结合起来。 Both of them must be performed. 他们两个都必须执行。 I tried to add an AND operator to between them but & isn't allowed. 我试图在它们之间添加AND运算符,但不允许。

How can i do this? 我怎样才能做到这一点? Are there any operator in regex substitutes of & ? &的正则表达式替代中是否有任何运算符? If there aren't, How can i do this same job by different ways? 如果没有,我该如何以不同的方式完成这项工作?

EDIT: Someone can't get my question so I decided to explain in more details. 编辑:有人无法理解我的问题,所以我决定详细解释。

(^(((?:abc|bcd|cde|def|efg|fgh|ghi|hij|ijk|jkl|klm|lmn|mno|nop|opq|pqr|qrs|rst|stu|tuv|uvw|vwx|wxy|xyz|012|123|234|345|456|567|678|789|(0|1|2|3|4|5|6|7|8|9|a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z)'\'1{2})).)*|^['\'x20-\x7E]+$)

You can try this regex from https://regex101.com/ 您可以从https://regex101.com/尝试使用此正则表达式

This is not working the way i want. 这不是我想要的方式。 3 consecutive characters aren't allowed ok but it is not checking non-english characters. 不允许连续3个字符,但不能检查非英语字符。 For example if you type ğğğ That will be accepted. 例如,如果您键入ğğğ,那将被接受。 I put | 我放| operator between them. 他们之间的运算符。 It's wrong I know. 我知道错了。 I gotta put AND operator but I don't know How can AND operator be used in a regular expression? 我必须放置AND运算符,但不知道如何在正则表达式中使用AND运算符? That's actually the main problem. 这实际上是主要问题。

You can use the following: 您可以使用以下内容:

^(?!.*(?:abc|bcd|cde|def|efg|fgh|ghi|hij|ijk|jkl|klm|lmn|mno|nop|opq|pqr|qrs|rst|stu|tuv|uvw|vwx|wxy|xyz|012|123|234|345|456|567|678|789|([\da-z])\1{2}))[\x20-\x7E]+$

正则表达式可视化

Demo 演示

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

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