Mike Pone
Mike Pone

Reputation: 19320

Regex ending with pipe character

How does it affect a regex to have it just end with a pipe character?

example :

[0-9]{1,5}|

I know the pipe means or, but in this case it's not "orring" anything. An online tool such as http://regexpal.com/ seems to ignore the extraneous pipe character. Does anyone know if a regex spec can say anything about how a trailing pipe is supposed to be treated?

Upvotes: 1

Views: 1425

Answers (3)

anubhava
anubhava

Reputation: 784998

Your regex:

[0-9]{1,5}|

is equivalent to:

(?:[0-9]{1,5})?

i.e. match 5 digits or match nothing (empty text).

Upvotes: 0

Barmar
Barmar

Reputation: 780798

It's ORing with an empty string. So it will match an empty input.

Upvotes: 0

Jerry
Jerry

Reputation: 71538

It's still treated as 'or', in your case, it just means it should match 1 to 5 digits, or an empty string.

Note that an empty string can be matched in practically any string.

Upvotes: 4

Related Questions