正则表达式仅接受 5 个数字,然后是打字稿上的破折号或字母

发布于 2025-01-10 12:16:19 字数 723 浏览 0 评论 0 原文

我正在处理正则表达式的问题。 我有一个 maxLength 10 的输入。

到目前为止,我实现了第一个给定值可以是数字,例如 12345,但随后它等待破折号,在它之后您可以写一个字母或再次写一个数字 maxLength=10 例如: 12345-a121 是允许的,它适用于当前的

但我希望在 5 位数字之后可以允许字母或破折号,因为目前使用此正则表达式,只允许在后面加上破折号5 位数字。 例如 12345a 或 12345- 是允许的。 这是我正在使用的实际正则表达式。

Valid/Matches: 12345a235, 123a, 12345-aa1, 12345a, 12345-a.  

Not Valid/Does not matches: -11, 111111, aaaa, 

(?=^[^W_]{1,5}-[^W_]{1,8}$)^.{1,10}$|^[^W_]{1,5}$

我正在 regex101.com 上进行调试,但我没有找到允许的方法。 例如12345a

这是检查是否匹配的条件。

if (!this.value.toString().match('^\d{1,5}(?!\d+)[-\p{L}\d]+$') && this.value.toString()) {
      return ValidationInfo.errorCode("You need to do something");

谢谢你的帮助

I am dealing with an issue with Regex.
I have a input which has maxLength 10.

I achieved till now to have the first given value can be digits for example 12345 but then it waits for a dash and after it you can write a letter or again a number maxLength=10 for example: 12345-a121 is allowed and it works with the currrent

But I want to be possible after the 5 digits to be allowed letters or dash because for the moment with this regex it is allowed only dash after 5 digits.
For example 12345a or 12345- to be allowed.
This is the actual regex what I am using.

Valid/Matches: 12345a235, 123a, 12345-aa1, 12345a, 12345-a.  

Not Valid/Does not matches: -11, 111111, aaaa, 

(?=^[^W_]{1,5}-[^W_]{1,8}$)^.{1,10}$|^[^W_]{1,5}$

I am debugging on the regex101.com but I am not finding a way for that to allow.
12345a for example

This is the condition to check if it matches or not.

if (!this.value.toString().match('^\d{1,5}(?!\d+)[-\p{L}\d]+

Thank you for the help

) && this.value.toString()) { return ValidationInfo.errorCode("You need to do something");

Thank you for the help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

死开点丶别碍眼 2025-01-17 12:16:19

编辑,因为第一种方法的模式可以简化,并且也缺少结束序列长度的限制。

示例代码...

const multilineSample = `12345a235
123a
12345-aa1
12345a
12345-a

12-a235dkfsf
12-a235dkfsfs

123a-dssava-y
123a-dssava-1a

12345-aa1--asd-
12345-aa1--asd-s

-11
111111
aaaa`;

// see ... [https://regex101.com/r/zPkcwv/3]
const regXJustMatch = /^\d{1,5}[-\p{L}][-\p{L}\d]{0,9}$/gmu;

// see ... [https://regex101.com/r/zPkcwv/4]
const regXNamedGroups =
  /^(?<digitsOnly>\p{N}{1,5})(?<miscellaneous>[-\p{L}][-\p{L}\p{N}]{0,9})$/gmu;

console.log(
  'matches only ...',
  multilineSample.match(regXJustMatch)
);
console.log(
  'matches and captures ...', [
    ...multilineSample.matchAll(regXNamedGroups)
  ]
  .map(({ 0: match, groups }) => ({ match, ...groups }))
);
.as-console-wrapper { min-height: 100%!important; top: 0; }


第一种方法

对于两者变体很明显以...开头,

  • 至少由 1 位数字和最多 5 位数字组成的数字序列...

这也很清楚,一个人想要以任何一个字符序列结束短跑和/或词。由于必须排除 _,因此不能仅使用 \w 转义字母和数字,因为 \w 覆盖/包含 _ 也是如此。但是可以使用 unicode 属性转义,因此...

  • 用有效字符类覆盖行尾的正则表达式是...
    • 已经混合... [-\p{L}\d]+$
    • 主要是unicode转义... [-\p{L}\p{N}]+)$

组合正则表达式如 ... /^\d{1,5}[-\p{L}\d]+$/u ... 几乎涵盖了要求,但失败了111111 哪个原因得到匹配,即使它不应该符合到要求。

负向预测 ... (?!\d+)< /code> 分别 (?!\p{N}+) ...紧随起始数字序列确实会阻止任何其他(终止)纯数字序列,因此 123456 未匹配不再了。

示例代码...

const multilineSample = `12345a235
123a
12345-aa1
12345a
12345-a

-11
111111
aaaa`;

// see ... [https://regex101.com/r/zPkcwv/1]
const regXJustMatch = /^\d{1,5}(?!\d+)[-\p{L}\d]+$/gmu;

// see ... [https://regex101.com/r/zPkcwv/2]
const regXNamedGroups =
  /^(?<digitsOnly>\p{N}{1,5}(?!\p{N}+))(?<miscellaneous>[-\p{L}\p{N}]+)$/gmu;

console.log(
  'matches only ...',
  multilineSample.match(regXJustMatch)
);
console.log(
  'matches and captures ...', [
    ...multilineSample.matchAll(regXNamedGroups)
  ]
  .map(({ 0: match, groups }) => ({ match, ...groups }))
);
.as-console-wrapper { min-height: 100%!important; top: 0; }

Edit since the patterns of the first approach can be simplified and also were missing the limitations of the ending sequence's length.

Example code ...

const multilineSample = `12345a235
123a
12345-aa1
12345a
12345-a

12-a235dkfsf
12-a235dkfsfs

123a-dssava-y
123a-dssava-1a

12345-aa1--asd-
12345-aa1--asd-s

-11
111111
aaaa`;

// see ... [https://regex101.com/r/zPkcwv/3]
const regXJustMatch = /^\d{1,5}[-\p{L}][-\p{L}\d]{0,9}$/gmu;

// see ... [https://regex101.com/r/zPkcwv/4]
const regXNamedGroups =
  /^(?<digitsOnly>\p{N}{1,5})(?<miscellaneous>[-\p{L}][-\p{L}\p{N}]{0,9})$/gmu;

console.log(
  'matches only ...',
  multilineSample.match(regXJustMatch)
);
console.log(
  'matches and captures ...', [
    ...multilineSample.matchAll(regXNamedGroups)
  ]
  .map(({ 0: match, groups }) => ({ match, ...groups }))
);
.as-console-wrapper { min-height: 100%!important; top: 0; }


1st approach

For both variants it is obvious to start with ...

  • a digit sequence of at least 1 and up to 5 digits ...

It's also clear, one wants to end with a character sequence of any of dash and/or word. Due to having to exclude _ one can not just use the \w escape for letters and digits since \w covers/includes _ as well. But one could use unicode property escapes, thus ...

  • a regex covering the end of a line with a valid character class is ...
    • already mixed ... [-\p{L}\d]+$
    • mostly unicode escapes ... [-\p{L}\p{N}]+)$

A combined regex like ... /^\d{1,5}[-\p{L}\d]+$/u ... almost covers the requirements but fails for 111111 which of cause gets matched even though it shouldn't according to the requirements.

A negative lookahead ... (?!\d+) respectively (?!\p{N}+) ... which follows the starting digit sequence does prevent any other (terminating) digit-only sequence, thus 123456 does not get matched anymore.

Example code ...

const multilineSample = `12345a235
123a
12345-aa1
12345a
12345-a

-11
111111
aaaa`;

// see ... [https://regex101.com/r/zPkcwv/1]
const regXJustMatch = /^\d{1,5}(?!\d+)[-\p{L}\d]+$/gmu;

// see ... [https://regex101.com/r/zPkcwv/2]
const regXNamedGroups =
  /^(?<digitsOnly>\p{N}{1,5}(?!\p{N}+))(?<miscellaneous>[-\p{L}\p{N}]+)$/gmu;

console.log(
  'matches only ...',
  multilineSample.match(regXJustMatch)
);
console.log(
  'matches and captures ...', [
    ...multilineSample.matchAll(regXNamedGroups)
  ]
  .map(({ 0: match, groups }) => ({ match, ...groups }))
);
.as-console-wrapper { min-height: 100%!important; top: 0; }

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文