英文:
How to Google form RegEx validation that matches with the "OR" expression in two patterns
问题
我想在谷歌表单中验证斯里兰卡的车辆注册号码。
这里,注册号码由一个"-"
符号分成两部分。
"-"
符号左侧的部分。
两到三位数字。或两到三个英文字母。数字和字母永不混合。
"-"
符号右侧的部分。
始终是4位数字。
以下是一些示例案例。
12-1234
,123-1234
,AB-1234
,ABC-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.
([0-9A-Za-z]{2})$(?[0-9A-Za-z]?)$\-[0-9]{4}
^[0-9]{2,3}$|^[A-Za-z]{2,3}$\-[0-9]{4}
[0-9]{2}?[0-9]?-[0-9]{4}
[0-9A-Za-z]{2}?[0-9A-Za-z]?\-[0-9]{4}
[0-9]{2,3}-[0-9]{4}||[a-zA-Z]{2,3}-[0-9]{4}
[0-9]{2,3}-[0-9]{4}|[a-zA-Z]{2,3}-[0-9]{4}
[a-zA-Z]{2}?[a-zA-Z]?[0-9]{2}?[0-9]?\-[0-9]{4}
^([a-zA-Z]{2,3}\-[0-9]{4})$
^([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}$
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论