mbrc
mbrc

Reputation: 3953

Java regex plus at the beginning is optional

I would like to write Java regex where plus at the beginning is optional

I try this but not working correctly

[+]+[0-9]{3,}

so that +123 and 123 is valid

What I am doing wrong?

Upvotes: 1

Views: 55

Answers (1)

Moishe Lipsker
Moishe Lipsker

Reputation: 3034

As Hamza commented below, use [+]?[0-9]{3,}. A question mark means one or none of the previous, which in this case means one or no + before the three numbers.

Upvotes: 2

Related Questions