How do I make part of a regex match optional?

You have the #p outside of the capturing group, which makes it a required piece of the result. You are also using the dot character (.) improperly. Dot (in most reg-ex variants) will match any character. Change it to:

([0-9]*)(?:#p([0-9]*))?

The (?:) syntax is how you get a non-capturing group. We then capture just the digits that you’re interested in. Finally, we make the whole thing optional.

Also, most reg-ex variants have a \d character class for digits. So you could simplify even further:

(\d*)(?:#p(\d*))?

As another person has pointed out, the * operator could potentially match zero digits. To prevent this, use the + operator instead:

(\d+)(?:#p(\d+))?

Leave a Comment