user879559
user879559

Reputation: 59

Understanding a regex expression

This expression evaluates a string to see if every character is a digit. I don't understand the -?. I know that ? means once or no times, but I'm not sure what putting dash in front of it means.

-?\d+

Upvotes: 2

Views: 80

Answers (2)

Stanley De Boer
Stanley De Boer

Reputation: 5241

It is not a special character. The dash is there to allow negative numbers.

Upvotes: 6

Ivaylo Strandjev
Ivaylo Strandjev

Reputation: 70929

This is needed because an integer may be negative in which case it will start with a minus (-). So what you do here is to check for sequence of 1 or more digits optionally preceded by a single minus.

Upvotes: 7

Related Questions