How to Google form RegEx validation that matches with the "OR" expression in two patterns

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

How to Google form RegEx validation that matches with the "OR" expression in two patterns

问题

我想在谷歌表单中验证斯里兰卡的车辆注册号码。
这里,注册号码由一个"-"符号分成两部分。
"-"符号左侧的部分。
两到三位数字。或两到三个英文字母。数字和字母永不混合。

"-"符号右侧的部分。
始终是4位数字。

以下是一些示例案例。
12-1234123-1234AB-1234ABC-1234

以下是我在谷歌表单中尝试的最成功的正则表达式。但是在这里,左侧部分的数字和字母混合在一起。"|"符号在谷歌表单中不起作用。

[0-9A-Za-z]{2}?[0-9A-Za-z]?\-[0-9]{4}
英文:

I wanted to validate vehicle registration numbers in Sri Lanka in google form.
Here, the registration number is divided into two parts by a "-" sign.
The part to the left of the "-" sign.
Two or three digits. Or two or three English letters. Numbers and letters are never mixed.

The part to the right of the "-" sign.
Always 4 digits.

Below are some example cases.
12-1234, 123-1234, AB-1234, ABC-1234

The following was the most successful regular expression I tried in google form. But here the numbers and letters are mixed in the left part. "|" Sign not working in google form.

[0-9A-Za-z]{2}?[0-9A-Za-z]?\-[0-9]{4}

Below are the other expressions I tried. None of them succeeded properly.

  1. ([0-9A-Za-z]{2})$(?[0-9A-Za-z]?)$\-[0-9]{4}
  2. ^[0-9]{2,3}$|^[A-Za-z]{2,3}$\-[0-9]{4}
  3. [0-9]{2}?[0-9]?-[0-9]{4}
  4. [0-9A-Za-z]{2}?[0-9A-Za-z]?\-[0-9]{4}
  5. [0-9]{2,3}-[0-9]{4}||[a-zA-Z]{2,3}-[0-9]{4}
  6. [0-9]{2,3}-[0-9]{4}|[a-zA-Z]{2,3}-[0-9]{4}
  7. [a-zA-Z]{2}?[a-zA-Z]?[0-9]{2}?[0-9]?\-[0-9]{4}
  8. ^([a-zA-Z]{2,3}\-[0-9]{4})$
  9. ^([0-9]{2,3}\-[0-9]{4})$

can you help me

答案1

得分: 2

如果你不想允许数字和字母混合,你将需要使用OR,因此你可以使用以下正则表达式:

^(?:\d{2,3}|[a-zA-Z]{2,3})-\d{4}$

查看这个链接

在这里,(?:\d{2,3}|[a-zA-Z]{2,3}) 允许2到3位数字或2到3位字母,其余的正则表达式部分是你已经知道的部分。

英文:

If you don't want to allow numbers and letters mixed, you will have to use OR and hence you can use following regex,

^(?:\d{2,3}|[a-zA-Z]{2,3})-\d{4}$

Check this out

Here, (?:\d{2,3}|[a-zA-Z]{2,3}) allows either 2 to 3 digits or 2 to 3 letters and rest regex is trivial you already know that part.

huangapple
  • 本文由 发表于 2023年7月3日 11:42:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76601731.html
匿名

发表评论

匿名网友

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

确定