OTUser
OTUser

Reputation: 3848

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

Regex should match Passport Passport11111 Passport12345 but shouldn't match Passport1 Passport123 Passport123456

I tried using Passport\d{5*} but its not working. Can someone please help me?

Upvotes: 0

Views: 95

Answers (1)

Tomalak
Tomalak

Reputation: 338158

You almost had it.

Passport(\d{5})?

The parentheses capture their contents (i.e. 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})?

Upvotes: 4

Related Questions