正则表达式,不允许连续两个特殊字符,并且也不允许出现在开头和结尾。

huangapple go评论85阅读模式
英文:

Regular Expression for not allowing two consecutive special characters and also not in beginning and end

问题

以下是您要的翻译内容:

我正在寻找一个用于字符串的正则表达式,以满足以下条件:

  1. 只包含 A-Z、a-z、0-9、_、-、.
  2. 不以 _、-、. 开头或结尾。
  3. 不包含连续的特殊字符或它们的组合。
  4. 长度最大为 36,最小为 1。

正确示例:

abcd-efgH
1
a
123
abc
abc-asd-123-asd_asd.asd

错误示例:

-
abc-_asd
abc.
abc.-asd
123123-123123-ads--asd
091-asdsad---

我在周围搜索并找到了以下内容:

/^(?!.*[^\na-z0-9]{2})(?=.*[a-z0-9]$)[a-z0-9].*$/gim

但这允许所有特殊字符,而不仅仅是我检查的这三个。

英文:

I'm looking for a regex for a string to

  1. Contain only A-Z a-z 0-9 _ - .
  2. Not begin/end with _ - .
  3. Not containing consecutive special characters or their combination
  4. Max 36 length, minimum 1

Right

abcd-efgH
1
a
123
abc
abc-asd-123-asd_asd.asd

Wrong:

-
abc-_asd
abc.
abc.-asd
123123-123123-ads--asd
091-asdsad---

I seearched around and got this :-

/^(?!.*[^\na-z0-9]{2})(?=.*[a-z0-9]$)[a-z0-9].*$/gim

but this allows all special characters and not just the 3 that i checek

答案1

得分: 5

你可以在这个正则表达式中使用3个零宽断言:

^(?![-_.])(?!.*[-_.]{2})(?!.*[-_.]$)[-\w.]{1,36}$

正则表达式演示链接

正则表达式详解:

  • ^:开头
  • (?![-_.]):负向前瞻,不允许在开头出现 [-_.]
  • (?!.*[-_.]{2}):负向前瞻,不允许任何地方出现连续的两个 [-_.]
  • (?!.*[-_.]$):负向前瞻,不允许在结尾出现 [-_.]
  • [-\w.]{1,36}:匹配一个 [-a-zA-Z0-9_.] 字符,最少1个,最多36个
  • $:结尾
英文:

You may use this regex with 3 lookaheads:

^(?![-_.])(?!.*[-_.]{2})(?!.*[-_.]$)[-\w.]{1,36}$

RegEx Demo

RegEx Details:

  • ^: Start
  • (?![-_.]): Negative lookahead to disallow [-_.] at the start
  • (?!.*[-_.]{2}): Negative lookahead to disallow 2 consecutive [-_.] anywhere
  • (?!.*[-_.]$): Negative lookahead to disallow [-_.] at the end
  • [-\w.]{1,36}: Match a [-a-zA-Z0-9_.] character, min: 1, max: 36
  • $: End

答案2

得分: 1

你是说类似于:

    /^(?!.{37,})[a-z\d]+(?:[-._][a-z\d]+)*$/gim

查看在线演示[这里](https://regex101.com/r/10bmbN/6)

---

* `^` - 字符串起始锚点。
* `(?!.{37,})` - 负向先行断言,限制最少37个字符。
* `[a-z\d]+` - 至少一个字符在这个字符组内。
* `(?:` - 打开非捕获组。
    * `[-._]` - 单个字符在这个字符组内。
    * `[a-z\d]+` - 至少一个字符在这个字符组内。
    * `)*` - 关闭非捕获组并允许匹配零次或多次。
* `$` - 字符串结束锚点。
英文:

Do you mean something like:

/^(?!.{37,})[a-z\d]+(?:[-._][a-z\d]+)*$/gim

See the online demo


  • ^ - Start string ancor.
  • (?!.{37,}) - Negative lookahead for 37 characters or more.
  • [a-z\d]+ - At least a single character within this character group.
  • (?: - Open non-capturing group.
    • [-._] - A single character within this character group.
    • [a-z\d]+ - At least a single character within this character group.
    • )* - Close non-capturing group and match it zero or more times.
  • $ - End string ancor.

答案3

得分: 0

我认为解决这个问题最简单、直接且直观逻辑的方法如下:

/^([a-z\d]|(?<![-_])(?<!^)[-_](?!$)){2,36}$/gim

解释非常简单。

第一个字符可以是字母或数字 或者(|)第一个字符可以是 _-,因为前一个字符

a) 不是 _ 也不是 -

前一个字符的条件在 负回顾后查找 中:(?<![-_])

这意味着,使用当前光标位置,负回顾后查找(由 (?<! P) 模式表示,其中 P 是模式),只有在模式 P 不匹配时才可以(PS:回顾后查找不移动光标)
在这种情况下,模式 P 就是 [-_]。如果 P 后面不是 -_,那么 -_ 就是一个可选项!

b) _- 不在开头

紧接着我们又有一个只测试文本开头的负回顾后查找。P 就是 ^(表示文本开头的锚点)。负回顾后查找部分是 (?<!^)(P 就是 ^

c) _- 不在结尾

最后,我们有一个负前瞻后出现在 [-_] 实际模式之后。唯一的语法差异是剪切字符 &lt;?P,其中 P 是模式)。现在,正则表达式处理在向前搜索模式 P,而不移动光标。如果模式失败,没关系(负面的!)
在这种情况下,我们有 (?!$),其中 P$,表示文本末尾的锚点。

例如:
ABCDEFGHIJKLMNOPQRSTU-XYZ01234_56789 匹配
ABCDEFGHIJKLMNOPQRSTU-_XYZ0123456789 不匹配
也不匹配 ABCDEFGHIJKLMNOPQRSTU-XYZ012345678_
也不匹配 _BCDEFGHIJKLMNOPQRSTU-XYZ0123456789

在此在线查看 此处

英文:

I think the simplest and most direct way to solve this problem, in an intuitive and logical way, is the following:

/^([a-z\d]|(?&lt;![-_])(?&lt;!^)[-_](?!$)){2,36}$/gim

The explanation is pretty simple.

The first char can be letter or digit OR (|) the first character can be additionally be _ or -, since the previous char

a) is not _ neither -.

The previous char condition is in the negative lookbehind: (?&lt;![-_])

It means, using the current cursor position, a negative look behind (represented by (?&lt;! P) model, where P is a pattern) and it's ok just if pattern P don't (negative) fit (PS: Lookbehind don't move the cursor)
In this case, pattern P is just [-_]. If P behind is not - neither _, it means that - or _ are allowed as a option!

b) _ ou - is not in the start

Right after that we have another negative look behind that tests only the beginning of the text. P is just ^(the anchor that indicates start of the text). The look behind piece is (?&lt;!^) (P is just ^)

c) _ ou - is not in the end

Finally we have a negative look forward, but is appears after [-_] real pattern. The only syntax difference is cut "<" character (?!P, where P is the pattern). Now, the regex processing is search forward for the pattern P, without move the cursor. If the pattern fail is OK (negative!)
In the case, we have (?!$), where P is $, the anchor that indicates the end of the text.

for example:
ABCDEFGHIJKLMNOPQRSTU-XYZ01234_56789 matches
but ABCDEFGHIJKLMNOPQRSTU-_XYZ0123456789 doesn't match
neither ABCDEFGHIJKLMNOPQRSTU-XYZ012345678_
neither _BCDEFGHIJKLMNOPQRSTU-XYZ0123456789

See online here:

huangapple
  • 本文由 发表于 2020年7月23日 23:23:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/63057698.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定