Mr. Mouse Mikołaj
Mr. Mouse Mikołaj

Reputation: 101

Why my regex accept also letters? ^[0-9\-\+_ ]

Why my regex accept also letters? For example:

It should accept string consisting of 0-9 and these special chars: +, _,-," "(space)

Upvotes: 1

Views: 122

Answers (1)

solarissmoke
solarissmoke

Reputation: 31474

Your regex is only testing the start of the string - in fact it is only testing the first character of the string. If [0-9\-\+_ ] is all you want in the whole string then stick a +$ at the end:

regex=r'^[0-9\-\+_ ]+$'

This says that the whole string, start to finish, is only allowed to contain the characters inside the square brackets.

Upvotes: 4

Related Questions