0

This is what my professor is asking of us; Write a PHP regular expression pattern that matches a string that satisfies the following description:

  1. The string must begin with the (uppercase) letter A.
  2. Any three alphanumeric characters must follow.
  3. After these, the letter B (uppercase of lowercase) must be repeated one or more times.
  4. The string must end with two digits.

Regex:

^A[[:alnum:]]{3}[Bb]+[[:digit:]]{2}$

I found this other version online, however it doesn't technically start with the letter A. I guess technically neither did mine...

[[:< : ]](A[[:alpha:]]{3}+[bB]+[a-zA-Z0-9]+[[:digit:]]{2})[[:>:]]
chris85
  • 23,846
  • 7
  • 34
  • 51
Mereinid
  • 295
  • 1
  • 3
  • 5
  • 1
    As I understand it, the string being tested must start with the letter `A`, (to pass), not the regex statement itself. As such, I'm unclear of your question. Also, you can use an online regex checker (Google will bring up several) to easily test out your regex expression against various strings to ensure it gives you the results you want. – SherylHohman Sep 25 '17 at 20:17
  • That looks good to me, but why not test it yourself? Make a list of test strings covering every case where you think there might be an issue, and make sure they all give the correct response from [preg_match()](https://secure.php.net/manual/en/function.preg-match.php). – Hayden Schiff Sep 25 '17 at 20:18
  • It is correct: **https://regex101.com/r/Wp1zbe/2** To shorten your expression, you could use `^A\w{3}[Bb]\d{2}$` which would allow [A-Za-z0-9_] for the alnum part (alnum + _, that is), see a demo for this one here: **https://regex101.com/r/Wp1zbe/3/** – Jan Sep 25 '17 at 20:20
  • AHH! I didn't know about the testing site. This is my first week..well I should say first day of php. It's an online course and just activated last night 11:59pm cst. I am amazed I took enough from reading the chapter to put up what I did. HaydenSchiff- I wish I knew how to make a list of strings covering every case... lol. (I wish this was a course in the classroom..I detest online courses.) Thank you all @jan, SherylHohman and HaydenSchiff. I'll go ahead and submit and see what we see. You'r all the man! ;P – Mereinid Sep 25 '17 at 20:29

0 Answers0