如何构建该正则表达式 [英] How to build that regex

查看:71
本文介绍了如何构建该正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨专家,



这个正则表达式模式

  string  .Format ( (?!。*(^ [.-] | [.-] $ | \\。 \\\。))(^ [a-zA-Z0-9 .-] {{{0},{1}}} $),minLength,maxLength); 

适用于

- 大写和小写字母加上句号和短划线

- 开始或结束时没有句号或短划线

- 没有双重句号

- 给定的最小值和最大值之间的长度



现在我如何在RegEx中说如果有 \。 ,然后在它和 $ 之间,可能存在 [a-zA-Z - ] + (但没有 [0-9] 了吗?







我做了一些研究,并提出了这种模式:

  string  enrichedPattern = < span class =code-keyword> string  .Format(
(?! ^ - )(^ ([a-zA-Z0-9 - ] + \\。)*([a-zA-Z] +)$)){{{0},{1}}}
minLength,maxLength
);

忽略C#对模式的开销,它变成

((?!^ - )( ^([a-zA-Z0-9 - ] + \。)*([a-zA-Z] +)$)){MinLength,MaxLength}

应该做什么:

有四个部分。

1)(?!^ - )(无论如何)

2)([a-zA-Z0-9 - ] + \。)*

3 )([a-zA-Z] +)$

4)(无论如何){MinLength,MaxLength}



3)是最重要的部分,因为它是唯一的强制性部分。它仅包含alphas。它可以是唯一的部分,或者以2)的实例开头,以句点分隔。

示例: MyDoMaIn



2)包含字母数字和短划线。它可以扩展1)更多的组,通过句点分隔。

示例: MyDoMaIn22.com sub.domain



1)禁止以破折号开头。所以没有 -sub.domain 了。设为 sub.domain yellow.-sub.domain



4)将总长度限制在 MinLength MaxLength

<之间br />
这是我的问题:只要 MinLength 至少有一个,前面提到的工作正常(或者我没有找到一个反面的例子)但是)

但是的最小值为零,似乎一切都是允许的。开始和结束时的周期/短划线,最后一部分中的数字,甚至是仅限数字的东西。



如果最小长度为零,那么RegEx的工作原理是什么?它让一切都通过了吗?

我怎样才能绕过它,所以即使允许零长度,如果有与模式匹配的东西,也会应用规则?

[/ Edit3]

解决方案

| \\.\\。))(^ [a-zA-Z0-9 .-] { {{0},{1}}}


),minLength,maxLength);

适用于

- 大写和小写字母加上句号和短划线

- 开始或结束时没有句号或短划线

- 没有双重句号

- 介于两者之间给出最小值和最大值



现在我怎么说在RegEx如果有 \。,那么之间它和


,可能存在 [a-zA-Z - ] + (但没有 [0-9] 了吗?







我做了一些研究并提出了这种模式:

  string  enrichedPattern =  string  .Format(
((^ - ?!)(^([A-ZA-Z0-9 - ]。+ \\)*([A-ZA-Z] +)

Hi experts,

this regex pattern

string.Format("(?!.*(^[.-]|[.-]$|\\.\\.))(^[a-zA-Z0-9.-]{{{0},{1}}}$)", minLength, maxLength);

works for
- upper- and lowercase letters plus period and dash
- no period nor dash at start or end
- no double periods
- length between given min and max

Now how do I say in RegEx "If there is \., then between it and $, there may exist [a-zA-Z-]+ (but no [0-9] anymore)?"


[Edit3]
I did some more research and came up with this pattern:

string enrichedPattern = string.Format(
    "((?!^-)(^([a-zA-Z0-9-]+\\.)*([a-zA-Z]+)$)){{{0},{1}}}",
    minLength, maxLength
);

Ignoring C#'s overhead to the pattern, it becomes
((?!^-)(^([a-zA-Z0-9-]+\.)*([a-zA-Z]+)$)){MinLength,MaxLength}
What it should do:
There are four parts.
1) (?!^-)(whatever)
2) ([a-zA-Z0-9-]+\.)*
3) ([a-zA-Z]+)$
4) (whatever){MinLength,MaxLength}

3) Is the most important part since it is the only mandatory one. It contains alphas only. It can be the only part or be preceded by an instance of 2) separated by a period.
Examples: MyDoMaIn.

2) Contains alphanumerics and dashes. It can extend 1) by more groups, separated through periods.
Examples: MyDoMaIn22.com or sub.domain

1) forbids to start with a dash. So no -sub.domain any more. Make it sub.domain or yellow.-sub.domain

4) limits the overall length to between MinLength and MaxLength

And here is my problem: As long as MinLength is at least one, the forementioned works as expected (or I haven't found a negative example yet).
But with a minimum value of zero, it seems that everything is allowed. Periods/dashes at start and end, numbers in the last part, even number-only stuff.

How does that RegEx thing work that with a minimum length of zero, it lets everything pass?
And how can I get around that, so that even if zero length is allowed, the rules are applied if there is something to match the pattern against?
[/Edit3]

解决方案

|\\.\\.))(^[a-zA-Z0-9.-]{{{0},{1}}}


)", minLength, maxLength);

works for
- upper- and lowercase letters plus period and dash
- no period nor dash at start or end
- no double periods
- length between given min and max

Now how do I say in RegEx "If there is \., then between it and


, there may exist [a-zA-Z-]+ (but no [0-9] anymore)?"


[Edit3]
I did some more research and came up with this pattern:

string enrichedPattern = string.Format(
    "((?!^-)(^([a-zA-Z0-9-]+\\.)*([a-zA-Z]+)


这篇关于如何构建该正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆