简体   繁体   English

5位或无数字字符串的正则表达式

[英]Regular Expression for a string with 5 or no digits

Am trying to write a regex that captures a string followed by exactly 5 or no digits 我正在尝试编写一个正则表达式来捕获字符串,该字符串后面紧跟5个数字或不包含数字

Regex should match Passport Passport11111 Passport12345 but shouldn't match Passport1 Passport123 Passport123456 正则表达式应该与Passport Passport11111 Passport12345但不应与Passport1 Passport123 Passport123456

I tried using Passport\\d{5*} but its not working. 我尝试使用Passport \\ d {5 *},但无法正常工作。 Can someone please help me? 有人可以帮帮我吗?

You almost had it. 你差点就吃了。

Passport(\d{5})?

The parentheses capture their contents (ie they create a group), but they also provide a way to make a part of the pattern atomic, so you can - for example - make it optional with ? 括号捕获了它们的内容(即,它们创建了一个组),但是它们也提供了一种使模式的一部分成为原子的方式,因此您可以-例如-使它成为可选内容? .

BTW, you can write parentheses that don't capture but make atomic only by adding an option to them: 顺便说一句,您可以编写不捕获但仅通过向其添加原子就可以使其成为原子的括号:

Passport(?:\d{5})?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM