http://www.perlmonks.org?node_id=11132741


in reply to Need a regular expression to check the first digit

Let's analyze this regex:

/^[5]+$/

Taken in full, you're saying starting from the beginning and ending at the end of the target string, match one or more 5 characters, and nothing else. You didn't allow for the trailing digits. Since you anchor to the end of the string, you're asserting that the string can only contain a bunch of 5s.

If you want to allow for eight trailing digits that are not 5s, you need to specify that too:

/^5\d{8}$/

Now you're saying the target has to start with a 5 and then must match eight more digits, at which point the target string must end.


Dave