hannah ajayi
hannah ajayi

Reputation: 7

regular expressions regex

I would like some help with this problem that i have been struggling with writing a regular expression that matches a string that begins with a digit and ends with a digit and has 3 or more alphabetic characters between the two digits (no other special characters between the two digits).

Tested it on:

4trtrtr8 (match)

5yy&&7 (no match, there are special characters there!)

4VVB3 (match)

5wEwHSJDKLJFAKLJFsdsafasfa2(match)

3rt5 (no match, there are only 2 characters between the digits)

This is what i have as my answer for this problem...

^[0-9].*([A-Z]|[a-z]).*[0-9]$

the only problem is that i need this to give a no match for the ones that say so above

Upvotes: 1

Views: 29

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626748

Your regex fails to cope with the task due to .*([A-Z]|[a-z]).* part that matches any 0+ chars, followed with 1 upper- or lowercase ASCII letter, and then having again any 0+ chars.

You need to use

^[0-9][a-zA-Z]{3,}[0-9]$

See the regex demo

Details

  • ^ - start of string
  • [0-9] - a digit
  • [a-zA-Z]{3,} - 3 or more (due to the limiting quantifier {3,}) ASCII letters
  • [0-9] - a digit at the...
  • $ - end of the string.

Upvotes: 2

Related Questions